我正在练习信号,我已经从gcc转移到ubuntu上的eclipse,我遇到了一些问题。下面的代码为gcc编译精细的eclipse我得到错误 "错误:从类型'void(*)(int)&#34中分配类型'union'时出现不兼容的类型;
在线查看由于编译器使用预C99版本,我发现错误的可能性。所以我尝试为C99版本编译eclipse,我在下面找到了链接How do you configure GCC in Eclipse to use C99?
我尝试按照SO链接中的建议进行更改,但目前我的其他标志有行说明 " -c -fmessage-length = 0"
如果我按照帖子的建议添加-std = c99之前或之后该行编译器无法找到文件本身
由于使用预C99版本的编译器,我假设我收到错误。请纠正我,如果我在错误的方向追逐,如果正确的话,正确的方法是将-std = c99添加到标志选项以使eclipse使用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 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;
}
答案 0 :(得分:1)
修改CFLAGS,(打开所需的项目)。
project
properties
C/C++Build
标签 - 展开Settings
C/C++Build
标签
GCC C Comiler
标签 - 展开miscellaneous
标签选择第三列-std=gnu99
ok
这可能需要为每个项目完成