在Linux中使用signal.h编译错误

时间:2012-01-13 04:45:04

标签: c linux signals system-calls signal-handling

我正在编写一个必须处理信号的shell程序。我的相关信号处理相关代码如下:

#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'

有谁能告诉我为什么我会收到这些警告和错误?

2 个答案:

答案 0 :(得分:12)

如果从编译行中删除-ansi,它将起作用。我怀疑问题是当你指定-ansi时,信号库的posix部分不包括在内。

如果您确实不想禁用-ansi,还可以将-D_POSIX_C_SOURCE添加到编译器选项中。

Here is a short discussion of ANSI and the gcc feature test macros

答案 1 :(得分:1)

请在“signal.h”之前尝试移动“sys / types.h”

如果这不起作用,请尝试添加:

#include <bits/sigaction.h>

如果仍然不起作用,请注明:

1)您的操作系统和版本

2)你的gcc版本