在 Linux 中使用 signal.h 编译错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8846047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
compile errors using signal.h in Linux
提问by Ataraxia
I'm writing a shell program that must handle signals. My relevant signal handling related code is as follows:
我正在编写一个必须处理信号的 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 );
Compiling gives me the following warnings and errors:
编译给了我以下警告和错误:
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'
Can anyone tell me why I'm getting these warnings and errors?
谁能告诉我为什么会收到这些警告和错误?
采纳答案by Timothy Jones
It will work if you remove -ansi
from your compile line. I suspect the problem is that the posix parts of the signal library aren't included when you specify -ansi
.
如果您-ansi
从编译行中删除,它将起作用。我怀疑问题是当您指定-ansi
.
If you really don't want to disable -ansi, you can also add -D_POSIX_C_SOURCE
to the compiler options.
如果你真的不想禁用-ansi,你也可以添加-D_POSIX_C_SOURCE
到编译器选项中。
Here is a short discussion of ANSI and the gcc feature test macros.
回答by paulsm4
Please try moving "sys/types.h" before"signal.h".
请尝试在“signal.h”之前移动“sys/types.h ”。
If that doesn't work, try adding this:
如果这不起作用,请尝试添加以下内容:
#include <bits/sigaction.h>
If it stilldoesn't work, please specify:
如果还是不行,请说明:
1) Your OS and version
1) 您的操作系统和版本
2) Your gcc version
2)你的gcc版本