Gcc #包括<;alsa/asoundlib.h>;及<;sys/time.h>;导致多定义冲突

Gcc #包括<;alsa/asoundlib.h>;及<;sys/time.h>;导致多定义冲突,gcc,compiler-errors,x86,c99,alsa,Gcc,Compiler Errors,X86,C99,Alsa,下面是要复制的最小C程序: #include <alsa/asoundlib.h> #include <sys/time.h> int main( void ) { } 在仍然使用-std=c99的情况下,如何解决此冲突?因为您的问题表明您正在使用GLIBC的time.h有一种方法可以通过告诉它不要定义timeval来避免此冲突。首先包括asoundlib.h,然后定义\u STRUCT\u TIMEVAL。在asoundlib.h中定义的将被使用 #include

下面是要复制的最小C程序:

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

int main( void )
{
}

在仍然使用
-std=c99
的情况下,如何解决此冲突?

因为您的问题表明您正在使用GLIBC的
time.h
有一种方法可以通过告诉它不要定义
timeval
来避免此冲突。首先包括
asoundlib.h
,然后定义
\u STRUCT\u TIMEVAL
。在
asoundlib.h
中定义的将被使用

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

int main( void )
{
}
#包括
#ifndef _STRUCT_TIMEVAL
#定义_STRUCT_TIMEVAL
#恩迪夫
#包括
内部主(空)
{
}

使用C99及更高版本,同一结构的定义不能重复。问题在于
alsa/asoundlib.h
包括
alsa/global.h
,其中包含以下代码:

/* 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

这是一堆旧的C代码和疯狂的宏。我让它工作的唯一方法是放弃并使用
-std=gnu11

您应该使用main的最低标准,即int main(void){return 0;}是的,您是对的(但这是另一行!)。
return 0
如果在C99中不存在,则是隐式的(这个问题被标记为Whcih)。请参阅:。无效是一个问题,因为问题集中在c99标准上,我会让它更符合标准。这是个坏主意,因为
main()
不应该是“特殊的”。但事实上,这在这里是完全无关的。顺便说一句,
intmain(void)
也很好。这是有效的-那么
std=c99
带来了什么,使我们需要显式禁用sys/time对timeval的定义?@Lombard它强制遵守标准,因此重新定义是非法的。试试
std=c89
,也会得到同样的结果。@Lombard:我对代码做了一个小小的修改,只定义了尚未定义的_STRUCT\u TIMEVAL。有一些警告选项,如
-Wall
,可能会将重新定义定义为问题。GCC没有时间。h。你可能是说格里伯。根据目标系统的不同,GCC可以使用多种libc实现中的任意一种。
/* 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
/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])
                                                             ^