Search code examples
gcccompiler-errorsx86c99alsa

#including <alsa/asoundlib.h> and <sys/time.h> results in multiple definition conflict


Here is the minimal C program to reproduce:

#include <alsa/asoundlib.h>
#include <sys/time.h>

int main( void )
{
}

This will compile with gcc -c -o timealsa.o timealsa.c, but if you include the -std=c99 switch, you get a redefinition error:

In file included from /usr/include/sys/time.h:28:0,
                 from timealsa.c:3:
/usr/include/bits/time.h:30:8: error: redefinition of ‘struct timeval’
 struct timeval
        ^
In file included from /usr/include/alsa/asoundlib.h:49:0,
                 from timealsa.c:2:
/usr/include/alsa/global.h:138:8: note: originally defined here
 struct timeval {
        ^

How can I resolve this conflict while still using -std=c99?


Solution

  • Since your question suggests you are using GLIBC's time.h there is a way to avoid this by telling it not to define timeval. Include asoundlib.h first then define _STRUCT_TIMEVAL. The one defined in asoundlib.h will be the one that gets used.

    #include <alsa/asoundlib.h>
    #ifndef _STRUCT_TIMEVAL
    #  define _STRUCT_TIMEVAL
    #endif
    #include <sys/time.h>
    
    int main( void )
    {
    }