2012-01-13 1 views
13

신호를 처리해야하는 셸 프로그램을 작성하고 있습니다.리눅스에서 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을 제거하면 작동합니다. 난 문제가 있다고 생각하면 신호 라이브러리의 posix 부분은 당신이 -ansi을 지정할 때 포함되지 않습니다.

정말로 -ansi를 비활성화하지 않으려면 -D_POSIX_C_SOURCE을 컴파일러 옵션에 추가 할 수 있습니다.

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

+0

'-ansi'을 제거하면 다음과 같은 내용이 남습니다 : '/tmp/ccl4HmT7.o : 기능 메인': ' '/ home/stu1/s11/gaw9451/Courses/OS_3/os1shell2.c : 68 : 정의되지 않음' 'reference to handler_function'' 'collect2 : ld가 1 종료 상태를 반환했습니다. ' '-D_POSIX_C_SOURCE'를 추가하는 것은 똑같습니다. 이것에 대한 아이디어? – Ataraxia

+0

어디에서나 정의 된 'handler_function'이 있습니까? 귀하의 질문에 선언했지만, 거기에 대한 코드가 없습니다. 'void handler_function (int signal_id) {}'을 추가해야만 코드가 잘 컴파일됩니다. –

0

"sys/types.h" "signal.h"전에 이동하십시오.

문제가 해결되지 않으면, 추가 노력이 :

#include <bits/sigaction.h> 

여전히가 작동하지 않는 경우, 지정하십시오

2

1) 귀하의 OS 및 버전) gcc 버전

+0

도움이되지 못했습니다 ...하지만 좋은 제안이었습니다 – Ataraxia

+2

''을 직접 포함하지 마십시오. 이 작업을 수행하는 것에 반대하는 파일의 맨 위에는 "# 오류"가 있습니다. – Celada

+0

'uname -a' '리눅스 매사추세츠 2.6.32-37- 제네릭 # 81-Ubuntu SMP Fri Dec 2 20:35:14 UTC 2011 i686 GNU/Linux' 'gcc --version' 'gcc (우분투 4.4.3-4ubuntu5) 4.4.3 ' – Ataraxia