I am practicing signals and I have moved from gcc to eclipse on ubuntu and I am running into some problem. Below code for gcc compiles fine for eclipse I get error "error: incompatible types when assigning to type ‘union ’ from type ‘void (*)(int)’"
Looking online I see chances of error because of compiler using pre C99 version. So I tried to make eclipse compile for C99 version and I found below link How do you configure GCC in Eclipse to use C99?
I tried to make the change as suggested in SO link but currently my others flag has line which says " -c -fmessage-length =0 "
if I add -std=c99 as suggested by post before or after that line compiler is not able to find the file itself
I have assumed I am getting error because of compiler using pre C99 version. Please correct me if I am chasing in wrong direction and if its correct what would is the correct way to add -std=c99 to flag option to make eclipse use C99
Edit : Based on the answer when I changed sig_handler parameter I was able to compile without adding -std=c99 flag however adding that as suggested I run into compilation error. Below is compile line
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
n
#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;
}