struct sigaction不完整错误

lka*_*nab 13 c linux signals

虽然包括<signal.h>我得到一个错误,说这struct sigaction是一个不完整的类型.

我不知道该怎么做.

请帮忙

#include <signal.h>
struct sigaction act;

int main(int argc, char** argv)
{
    int depth;

    /* validate arguments number*/
    if(argc < 2)
    {
        printf("fatal error: please use arguments <MaxChild> <MaxDepth>\n");
        exit(1);
    }

    /* register the realtime signal handler for sigchld*/

/*173*/
    memset(&act,0,sizeof(act));
    act.sa_handler = sigproc;
    sigaction(SIGCHLD,  /* signal number whose action will be changed */
             &act,      /* new action to do when SIGCHLD arrives*/
             NULL);     /* old action - not stored */


    srand(time(NULL));
    depth = rand() % atoi(argv[2]); /* [0 maxDepth]*/

    RecursiveFunc(atoi(argv[1]), depth);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误消息:

proc.c: In function ‘main’:
proc.c:173:22: error: invalid application of ‘sizeof’ to incomplete type ‘struct sigaction’ 
proc.c:174:2: error: invalid use of undefined type ‘struct sigaction’
cc1: warnings being treated as errors
proc.c:175:2: error: implicit declaration of function ‘sigaction’
Run Code Online (Sandbox Code Playgroud)

pmg*_*pmg 13

只是

#define _XOPEN_SOURCE
Run Code Online (Sandbox Code Playgroud)

在代码中的任何其他行之前,或使用-D用于定义预处理程序符号的选项进行编译

gcc ... -D_XOPEN_SOURCE ...
Run Code Online (Sandbox Code Playgroud)

  • @lkanab:根据`man 7 feature_test_macros` 这个宏(或其他一些宏)可用于“防止非标准定义被公开”或“公开默认情况下未公开的非标准定义”。[POSIX 文档的附录 B](http://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xsh_chap02.html#tag_22_02_02) 对此进行了一些说明 (3认同)
  • 这是有效的,但你能解释一下为什么这是必要的吗? (2认同)

小智 6

我通过更改与 gcc 一起使用的 C 标准解决了这个问题。

我变了:gcc -std=c99 ...

对此:gcc -std=gnu99 ...