0

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;
}
Community
  • 1
  • 1
oneday
  • 629
  • 1
  • 9
  • 32
  • the posted code is missing `#include ` – user3629249 Jan 23 '16 at 07:45
  • regarding this line: `stdbuf(stdout, NULL);` I suspect you really wanted to use `setbuf()` – user3629249 Jan 23 '16 at 07:54
  • this line: `sa.sigaction_handler = sighandler;` should be written as: `sa.sa_handler = sighandler; – user3629249 Jan 23 '16 at 08:08
  • after adding the missing header file, correcting lines as commented above, then compiling in eclipse, with `gcc -O0 -g3 -pedantic -Wall -Wextra -Wconversion -c -fmessage-length=0 -std=c99 -MMD -MP" – user3629249 Jan 23 '16 at 08:16
  • when reading the man page for `sigaction()` you will see: `sigaction(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE` which means that (at least) 1 of the three items must be defined otherwise the header file will skip right over the line(s) that prototype the `sigaction()` function. (in general, using the parameter `-std=gnu99` or having the `#define appropriateName=value` as the first line of the source file will cause the `sigaction()` function to be prototyped. (I usually can use: `#define _GNU_SOURCE` to cause the header file(s) to produce everything) ` – user3629249 Jan 24 '16 at 00:42

1 Answers1

1

to modify the CFLAGS, (with the desired project open).

  1. click the menu item project
  2. click the pull down menu item properties
  3. click the C/C++Build tab in the left window - to expand
  4. click the Settings tab in the expanded C/C++Build
  5. click the GCC C Comiler tab in the second column - to expand
  6. click the miscellaneous tab in the second column to select the third column
  7. select 'Other flags` line
  8. enter the additional flags, like -std=gnu99
  9. click ok

this may need to be done for each project

user3629249
  • 16,402
  • 1
  • 16
  • 17
  • Sorry for late reply - I am able to compile without error without adding -std=c99 by changing //sa.__sigaction_handler = sighandler; to sa.sa_handler = sighandler; as suggested. I always wrote sa.sa_handler = sighandler while doing on gcc but on eclipse after typing sa. the options of member functions only showed __sigaction_handler so I went for it. So this gets it to compile – oneday Jan 23 '16 at 16:00
  • - Regarding steps suggested yes I have already tried them and as pointed by me in post when I enter additional flags it does not compile – oneday Jan 23 '16 at 16:03
  • 1
    This happens because you're using non-C99 functions and types - you're using posix. You could use -std=gnu99 instead of -std=c99 which will give you access to all GNU extensions including posix, or add the #define _POSIX_C_SOURCE=200809L – nos Jan 23 '16 at 16:38
  • @nos - yes -std=gnu99 option works ..thanks for the answer that solves the problem !! – oneday Jan 24 '16 at 02:14
  • @user3629249 - can u edit your answer to include parameter as suggested by "nos" for posix compliant code . I would go ahead and accept the answer so others can benefit as well – oneday Jan 24 '16 at 02:17