2011-06-27 368 views
10

儘管包括<signal.h>我收到一個錯誤,說struct sigaction是一個不完整的類型。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; 
} 

錯誤消息:

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’ 
+0

此代碼編譯我的機器上。你的編譯器是什麼? libc?編譯選項? – mripard

回答

12

只是

#define _XOPEN_SOURCE 

代碼中的任何其他行之前,或與-D選項定義預處理程序編譯符號

gcc ... -D_XOPEN_SOURCE ... 
+0

這工作,但你能解釋爲什麼這是必要的? – lkanab

+1

@lkanab:根據'man 7 feature_test_macros',可以使用此宏(或其他幾個)來「防止非標準定義暴露」或「暴露默認情況下未公開的非標準定義」。 [POSIX文檔的附錄B](http://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xsh_chap02.html#tag_22_02_02)稍微說了一下 – pmg

+0

這沒有用!我正在使用Android Studio 2.1和實驗性gradle插件0.7.2。任何想法爲什麼? – kristoffz

1

我解決了這個問題,通過改變我用gcc使用的C標準。

我改變:gcc -std=c99 ...

這樣:gcc -std=gnu99 ...