[英]Configuring C99 for eclipse for posix
我正在练习信号,并且我已经从gcc转到ubuntu上的日食,并且遇到了一些问题。 下面的gcc代码对于eclipse可以很好地编译,但出现错误“错误:从类型'void(*)(int)'分配给类型'union'时类型不兼容”
在网上查看,我看到由于使用C99之前版本的编译器而出错的可能性。 因此,我尝试将eclipse编译为C99版本,并在下面找到链接如何在Eclipse中配置GCC以使用C99?
我试图按照SO链接中的建议进行更改,但目前我的其他标志的行显示“ -c -fmessage-length = 0”
如果我在该行编译器之前或之后按照帖子的建议添加-std = c99,则找不到文件本身
我认为由于使用C99之前的版本的编译器而导致出现错误。 如果我追错了方向,并且正确,将-std = c99添加到flag选项以使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;
}
修改CFLAGS(打开所需的项目)。
project
properties
C/C++Build
标签-展开 C/C++Build
的Settings
选项卡 GCC C Comiler
标签-展开 miscellaneous
选项卡以选择第三列 -std=gnu99
ok
每个项目可能需要这样做
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.