Search code examples
c++clinuxsignalssigqueue

Can't build sigqueue example with gcc but g++ is ok?


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


Solution

  • 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;