2012-01-13 8 views
13

私はシグナルを処理するシェルプログラムを作成しています。次のように関連するコードを扱う私の関連する信号は次のとおりです。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' 

誰も教えてもらえますか?

答えて

12

コンパイルラインから-ansiを削除するとうまくいくはずです。あなたが-ansiを指定すると、シグナルライブラリのposix部分が含まれていないという問題があると思われます。

-ansiを無効にしたくない場合は、-D_POSIX_C_SOURCEをコンパイラオプションに追加することもできます。

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

+0

'-ansi'を削除すると、 ' /tmp/ccl4HmT7.o:function main': ' '/home/stu1/s11/gaw9451/Courses/OS_3/os1shell2.c:68:未定義 'ハンドラ関数への参照 ' ' collect2:ldが1を返しました。終了ステータス ' ' -D_POSIX_C_SOURCE'を追加することは同じです。これについての任意のアイデア? – Ataraxia

+0

どこにでも 'handler_function'が定義されていますか?あなたの質問では宣言されていますが、コードはありません。私は 'void handler_function(int signal_id){}'を追加しなければなりませんでした。そしてあなたのコードはうまくコンパイルされました。 –

0

"signal.h"の前に "sys/types.h" を移動してみてください。

問題が解決しない場合は、追加してみてくださいこの:

#include <bits/sigaction.h> 

それはまだが動作しない場合は、指定してください:

1)あなたのOSとバージョンを)あなたのgccのバージョン

+0

助けてくれませんでしたが、それは良い提案でした – Ataraxia

+2

」は絶対に含めないでください。ファイルの先頭に#エラーがありますが、これはあなたに嫌です。 – Celada

+0

'は、una​​me -a' "Linuxのマサチューセッツ2.6.32-37-一般的な#81-UbuntuのSMP金12月2日午後8時35分14秒UTC 2011のi686 GNU/Linuxの 'のgcc --version' 「GCC(Ubuntuの4.4.3-4ubuntu5)4.4.3 ' – Ataraxia

関連する問題