简体   繁体   中英

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 ac 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

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;

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

union sigval value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM