Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constants not loaded by compiler

Tags:

c

posix

time.h

I started studying POSIX timers, so I started also doing some exercises, but I immediately had some problems with the compiler. When compiling this code, I get some strange messages about macros like CLOCK_MONOTONIC. Those are defined in various libraries like time.h etc. but the compiler gives me errors as if they are not defined.

It is strange because I am using a Fedora 16, and some of my friends with Ubuntu get less compiler errors than I :-O

I am compiling with gcc -O0 -g3 -Wall -c -fmessage-length=0 -std=c99 -lrt

Here the errors I get:

  • struct sigevent sigeventStruct gives:

    storage size of ‘sigeventStruct’ isn’t known
    unused variable ‘sigeventStruct’ [-Wunused-variable]
    Type 'sigevent' could not be resolved
    unknown type name ‘sigevent’
    
  • sigeventStruct.sigev_notify = SIGEV_SIGNAL gives:

    ‘SIGEV_SIGNAL’ undeclared (first use in this function)
    request for member ‘sigev_notify’ in something not a structure or union
    Field 'sigev_notify' could not be resolved
    
  • if(timer_create(CLOCK_MONOTONIC, sigeventStruct, numero1) == -1) gives:

    implicit declaration of function ‘timer_create’ [-Wimplicit-function- declaration]
    ‘CLOCK_MONOTONIC’ undeclared (first use in this function)
    Symbol 'CLOCK_MONOTONIC' could not be resolved
    

Here is the code:

#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>

int main()
{
        timer_t numero1;
        struct sigevent sigeventStruct;
        sigeventStruct.sigev_notify = SIGEV_SIGNAL;
        if(timer_create(CLOCK_MONOTONIC, sigeventStruct, numero1) == -1)
        {
                printf( "Errore: %s\n", strerror( errno ) );
        }
        return 0;
}
like image 453
Germano Massullo Avatar asked Jan 16 '12 14:01

Germano Massullo


1 Answers

If you are using -std=c99 you need to tell gcc you're still using recent versions of POSIX:

#define _POSIX_C_SOURCE 199309L

before any #include, or even with -D on the command line.

Other errors:

  • Missing #include <string.h>
  • You need a pointer for timer_create, i.e. &sigeventStruct instead of just sigeventStruct
like image 200
Flexo Avatar answered Sep 30 '22 17:09

Flexo