#including <alsa/asoundlib.h>和<sys/time.h>会导致多个定义冲突

Lom*_*ard 5 x86 gcc compiler-errors c99 alsa

这是重现的最小C程序:

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

int main( void )
{
}
Run Code Online (Sandbox Code Playgroud)

这将编译gcc -c -o timealsa.o timealsa.c,但如果您包含该-std=c99开关,则会出现重新定义错误:

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 {
        ^
Run Code Online (Sandbox Code Playgroud)

如何在仍然使用时解决此冲突-std=c99

Mic*_*tch 5

由于您的问题表明您正在使用GLIBC,time.h因此可以通过告诉它不要定义来避免这种情况timeval.asoundlib.h首先包括然后定义_STRUCT_TIMEVAL.定义asoundlib.h的那个将是被使用的那个.

#include <alsa/asoundlib.h>
#ifndef _STRUCT_TIMEVAL
#  define _STRUCT_TIMEVAL
#endif
#include <sys/time.h>

int main( void )
{
}
Run Code Online (Sandbox Code Playgroud)

  • @Lombard它强制执行标准,因此重新定义是非法的.试试`std = c89`,会给你相同的结果. (3认同)