我正在編寫一個必須處理信號的shell程序。我的相關信號處理相關的代碼如下:在Linux中使用signal.h編譯錯誤
#include <signal.h>
...
#include <sys/types.h>
...
void installSigactions(int, struct sigaction*);
void handler_function(int signal_id);
...
/*define signal table*/
struct sigaction signal_action;
/*insert handler function*/
signal_action.sa_handler = handler_function;
/*init the flags field*/
signal_action.sa_flags = 0;
/*are no masked interrupts*/
sigemptyset(&signal_action.sa_mask);
/*install the signal_actions*/
sigaction(SIGINT, &signal_action, NULL);
編譯給了我下面的警告和錯誤:
gcc -Wall -ggdb -ansi -static -pedantic -o os1shell2 os1shell2.c
os1shell2.c:35: warning: 'struct sigaction' declared inside parameter list
os1shell2.c:35: warning: its scope is only this definition or declaration,
which is probably not what you want
os1shell2.c: In function 'main':
os1shell2.c:66: error: storage size of 'signal_action' isn't known
os1shell2.c:75: warning: implicit declaration of function 'sigemptyset'
os1shell2.c:78: warning: implicit declaration of function 'sigaction'
誰能告訴我爲什麼我得到這些警告和錯誤?
刪除'-ansi'會給我留下: '/tmp/ccl4HmT7.o:In main main':' '/ home/stu1/s11/gaw9451/Courses/OS_3/os1shell2.c:68:undefined' '對handler_function''的引用' 'collect2:ld返回1退出狀態' 添加'-D_POSIX_C_SOURCE'也是一樣的。對此有何想法? – Ataraxia 2012-01-13 05:08:16
你是否在任何地方定義了'handler_function'?它是在你的問題中聲明的,但是沒有代碼。我不得不添加'void handler_function(int signal_id){}',然後你的代碼編譯得很好。 – 2012-01-13 05:11:35