#including <alsa / asoundlib.h> and <sys / time.h> leads to a conflict with multiple definitions

Here is the minimum C program to play:

#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 turn on the -std=c99 switch, you will get an override 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 to resolve this conflict when using -std=c99 ?

+5
source share
2 answers

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

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

With C99 and later, you cannot duplicate definitions of the same structure. The problem is that alsa/asoundlib.h includes alsa/global.h , which contains this code:

 /* for timeval and timespec */ #include <time.h> ... #ifdef __GLIBC__ #if !defined(_POSIX_C_SOURCE) && !defined(_POSIX_SOURCE) struct timeval { time_t tv_sec; /* seconds */ long tv_usec; /* microseconds */ }; struct timespec { time_t tv_sec; /* seconds */ long tv_nsec; /* nanoseconds */ }; #endif #endif 

So Michael Patch's solution won't work - by the time you turned on alsa/asoundlib.h , it's too late. The correct solution is to define _POSIX_C_SOURCE ( _POSIX_SOURCE is deprecated). There is more information about these macros here and here .

For example, you can try -D_POSIX_C_SOURCE=200809L . However, if you do this, you will get errors like this:

 /usr/include/arm-linux-gnueabihf/sys/time.h:110:20: error: field 'it_interval' has incomplete type struct timeval it_interval; ^ /usr/include/arm-linux-gnueabihf/sys/time.h:112:20: error: field 'it_value' has incomplete type struct timeval it_value; ^ /usr/include/arm-linux-gnueabihf/sys/time.h:138:61: error: array type has incomplete element type extern int utimes (const char *__file, const struct timeval __tvp[2]) ^ 

This is all a big mess of old C code and macro madness. The only way I worked was to abandon the use of -std=gnu11 .

+1
source

All Articles