compile errors using signal.h in Linux

20,958

It will work if you remove -ansi from your compile line. I suspect the problem is that the posix parts of the signal library aren't included when you specify -ansi.

If you really don't want to disable -ansi, you can also add -D_POSIX_C_SOURCE to the compiler options.

Here is a short discussion of ANSI and the gcc feature test macros.

Share:
20,958
rurouniwallace
Author by

rurouniwallace

I code for a living, and also in my spare time. I like learning new things and taking on personal projects to keep my career and personal development from stagnating. Yay learning.

Updated on August 26, 2020

Comments

  • rurouniwallace
    rurouniwallace over 3 years

    I'm writing a shell program that must handle signals. My relevant signal handling related code is as follows:

    #include <signal.h>
    ...
    #include <sys/types.h>
    ...
    void installSigactions( int, struct sigaction* );
    
    void handler_function( int signal_id );
    ...
    /*define signal table*/
    struct sigaction signal_action;
    
    /*insert handler function*/
    signal_action.sa_handler = handler_function;
    
    /*init the flags field*/
     signal_action.sa_flags = 0;
    
    /*are no masked interrupts*/
    sigemptyset( &signal_action.sa_mask );
    
    /*install the signal_actions*/
    sigaction( SIGINT, &signal_action, NULL );
    

    Compiling gives me the following warnings and errors:

    gcc -Wall -ggdb -ansi -static -pedantic -o os1shell2 os1shell2.c
    os1shell2.c:35: warning: 'struct sigaction' declared inside parameter list
    os1shell2.c:35: warning: its scope is only this definition or declaration, 
    which is probably not what you want
    os1shell2.c: In function 'main':
    os1shell2.c:66: error: storage size of 'signal_action' isn't known
    os1shell2.c:75: warning: implicit declaration of function 'sigemptyset'
    os1shell2.c:78: warning: implicit declaration of function 'sigaction'
    

    Can anyone tell me why I'm getting these warnings and errors?

  • rurouniwallace
    rurouniwallace over 12 years
    removing -ansi leaves me with: /tmp/ccl4HmT7.o: In function main': /home/stu1/s11/gaw9451/Courses/OS_3/os1shell2.c:68: undefined reference to handler_function' collect2: ld returned 1 exit status Adding the -D_POSIX_C_SOURCE does the same. Any ideas on this??
  • Timothy Jones
    Timothy Jones over 12 years
    Do you have handler_function defined anywhere? It's declared in your question, but there's no code for it. I had to add void handler_function( int signal_id ) {}, and then your code compiled fine.