我正在練習信號,並且我已經從gcc移到eclipse上,並且遇到了一些問題。 「:分配給從類型鍵入‘聯盟’‘無效(*)(INT)’時不兼容類型的錯誤」爲eclipse配置C99 for posix
尋找網上我看到,因爲編譯器錯誤的機率低於GCC代碼編譯罰款月食我得到錯誤 使用pre C99版本。於是,我就使Eclipse編譯C99版,我發現下面的鏈接How do you configure GCC in Eclipse to use C99?
我試圖做出改變,如SO鏈接建議,但目前我的其他標誌具有線,說 「-c -fmessage長度= 0 「
如果我添加-std = C99作爲之前或之後行編譯器無法找到文件本身
我假設我使用預C99版得到,因爲編譯器的錯誤郵寄建議。請糾正我,如果我以錯誤的方向,如果它的正確我追什麼會是-std = C99添加到標誌選項,以使Eclipse中使用C99的正確方法
編輯:基於答案時,我改變了sig_handler參數我能夠在不添加-std = c99標誌的情況下進行編譯,但是按照建議,我會遇到編譯錯誤。下面是編譯行
GCC -O0 -g3 -Wall -c -fmessage長度= 0 -std = C99 -MMD -MP -MF「
`odd_process.d" -MT"odd_process.d" -o "odd_process.o" "../odd_process.c`"
../odd_process.c: In function ‘main’:
../odd_process.c:13:2: error: unknown type name ‘sigset_t’
../odd_process.c:14:19: error: storage size of ‘sa’ isn’t know
Ñ
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void sighandler(int sig)
{
printf("signal has been caught \n ");
}
int main(void)
{
sigset_t block_signal, empty_signal;
struct sigaction sa;
pid_t childid;
int i;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
**/*for below line while running on eclipse I see error which says*/
/***error: incompatible types when assigning to type ‘union <anonymous>’ from type ‘void (*)(int)’** */**
**sa.__sigaction_handler = sighandler;**
stdbuf(stdout, NULL);
if (sigaction(SIGCHLD, &sa, NULL) == -1)
{
printf("value not passed to signal handler \n ");
}
sigemptyset(&block_signal);
sigaddset(&block_signal, SIGCHLD);
if (sigprocmask(SIG_SETMASK, &block_signal, NULL) == -1)
{
printf("error occurred while setting signal mask \n");
}
childid = fork();
printf("value of child id is -- %d ", childid);
switch(childid)
{
case -1:
printf("Error condition child creation did not happen properly \n");
exit(-1);
case 0:
printf(" Child got created and will print odd number !!! ");
sleep(5);
exit(1);
default:
printf(" parent gets created !!! \n ");
break;
}
sigemptyset(&empty_signal);
sigsuspend(&empty_signal);
printf(" parent will print \n ");
for(i = 0; i < 10; i++)
{
printf("%d ", i);
}
printf("\n");
return EXIT_SUCCESS;
}
張貼代碼丟失的#include''關於 –
user3629249
這行做:'stdbuf(標準輸出,NULL );'我懷疑你真的想用'setbuf()' – user3629249
這一行:'sa.sigaction_handler = sighandler;'應該寫成:'sa.sa_handler = sighandler; – user3629249