views:

49

answers:

2

Hi

I have a strange build problem.

I have a simple test program that sends a sigqueue to another process.

This little code example builds and runs when I build it as a c++ program (compiled with g++) but when I compile it as a c program (with gcc) I get a error that he can't find the sigval struct.

The short example:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>

int main(int argc, char *argv[])
{
        sigval value;
        value.sival_int = 123;
        sigqueue(0,SIGUSR1, value);
}

Please note that I replaced the pid with 0 to simplify this question.

And if I compile with gcc I get this:

$> gcc sigusr1_mini.c 
sigusr1_mini.c: In function ‘main’:
sigusr1_mini.c:9: error: ‘sigval’ undeclared (first use in this function)
sigusr1_mini.c:9: error: (Each undeclared identifier is reported only once
sigusr1_mini.c:9: error: for each function it appears in.)
sigusr1_mini.c:9: error: expected ‘;’ before ‘value’
sigusr1_mini.c:10: error: ‘value’ undeclared (first use in this function)

What am I missing here, why can't he find the sigval struct? And why can g++ find it?

Thanks Johan

+4  A: 

In C, struct and union tags do not introduce names that can be used on their own like they do in C++. You must spell it out:

union sigval value;
caf
@caf: In C++ they aren't identifiers either, but can just be used as such if there is no ambiguity.
Jens Gustedt
@Jens Gustedt: Is that update accurate?
caf
@caf: Hm, difficult. I'd go for something like `... tags cannot be used in place of ordinary identifiers like they may in C++`.
Jens Gustedt
+1  A: 

How is sigval defined in h-file? C compiler may require full definition, for example:

union sigval value;

Alex Farber