#including <alsa asoundlib.h =“”>和<sys time.h =“”>导致多个定义冲突

时间:2015-09-19 19:28:00

标签: c c99 alsa

这是重现的最小C程序:

(intern "CAR")

这将使用#include <alsa/asoundlib.h> #include <sys/time.h> int main( void ) { } 进行编译,但如果您包含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 { ^ 时解决此冲突?

1 个答案:

答案 0 :(得分:1)

使用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

所以Michael Petch的解决方案不起作用 - 当你加入alsa/asoundlib.h时已经太晚了。正确的解决方案是定义_POSIX_C_SOURCE_POSIX_SOURCE已过时)。有关这些宏herehere的详细信息。

例如,您可以尝试-D_POSIX_C_SOURCE=200809L。但是,如果你这样做,你会得到这样的错误:

/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])
                                                             ^

这是旧C代码和宏观疯狂的大混乱。我开始工作的唯一方法是放弃并使用-std=gnu11