我正在练习信号,我已经从gcc移到了乌本图上,我遇到了一些问题。下面是gcc为eclipse编译的代码,我得到了错误" error :在类型‘void(*)(Int)’中为类型‘union’赋值时不兼容的类型。
在网上,我看到了错误的机会,因为编译器使用了预C99版本。因此,我尝试将eclipse编译成C99版本,并在下面的链接How do you configure GCC in Eclipse to use C99?中找到
我试图按SO link中的建议进行更改,但目前我的其他标志有一行,上面写着“-c -fmessage-length =0”。
如果我按照post的建议在该行之前或之后添加-std=c99,编译器将无法找到文件本身
我假设由于编译器使用了预C99版本而导致了错误。如果我在追逐错误的方向,请纠正我,如果它是正确的,那么添加-std=c99到标志选项的正确方式是使用C99。
编辑:基于答案的--当我更改sig_handler参数时,我能够编译而不添加-std=c99标志,但是,按照建议,我遇到了编译错误。下面是编译行
gcc -O0 -g3 -Wall -c -fmessage-length=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 knowN
#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;
}发布于 2016-01-23 07:23:53
修改CFLAGS,(打开所需的项目)。
projectpropertiesC/C++Build选项卡以展开Settings中的C/C++Build选项卡GCC C Comiler选项卡以展开miscellaneous选项卡以选择第三列-std=gnu99ok这可能需要在每个项目中完成。
https://stackoverflow.com/questions/34960039
复制相似问题