c - 在几个 header 中重新定义 'struct timeval'

标签 c alsa redefinition intel-edison libalsa

我在编译 C 程序以将声音从 Intel Edison 传输到设备(iOS 和 Android)时遇到了几个问题。

我做了一个 C 程序: 我在我的程序中使用 alsa/asoundlib.h 和 pthread.h 我不包括 sys/time.h 因为 ALSA 不允许这样做。

我在我的程序中使用了很多 timeval,当我在我的电脑上编译它时,我编译得很好,但是在我的爱迪生上,当我:

gcc -std=c99 -Wall -O0 -ggdb -o sender sender.c spsc_circular_queue.c -lopus -lasound -lpthread


In file included from /usr/include/alsa/asoundlib.h:49:0,
                 from sender.c:16:
/usr/include/alsa/global.h:145:8: error: redefinition of 'struct timespec'
 struct timespec {
        ^
In file included from /usr/include/alsa/global.h:34:0,
                 from /usr/include/alsa/asoundlib.h:49,
                 from sender.c:16:
/usr/include/time.h:120:8: note: originally defined here
 struct timespec
        ^
In file included from /usr/include/time.h:41:0,
                 from /usr/include/sched.h:34,
                 from sender.c:18:
/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 sender.c:16:
/usr/include/alsa/global.h:140:8: note: originally defined here
 struct timeval {
        ^
Makefile:16: recipe for target 'sender' failed
make: *** [sender] Error 1

我怎样才能设法阻止这些重新定义?! 感谢您的帮助!

额外信息:

我包括:

#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <alloca.h>
#include <limits.h>
#include <inttypes.h>
#include <alsa/asoundlib.h>
#include "../opus/include/opus.h"
#include <pthread.h>
#include "spsc_circular_queue.h"

我删除了 sched.h,没有任何反应

最佳答案

ALSA 依赖于 struct timespecstruct timeval 类型。因此,它的 global.h header 适本地执行此操作:

/* for timeval and timespec */
#include <time.h>

但是,似乎 GLIBC 仅在定义了适当的功能测试宏时才定义这些结构,因为该 header 还说:

#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

很难确定 GLIBC 在什么情况下确实声明了所需的结构。它确实有条件地这样做,但看起来这些条件,至少在 GLIBC v2.17 中,比 ALSA 假设的更普遍。因此 ALSA 似乎与 GLIBC 不同步,如果它确实在一开始就完全同步的话,并且在某些情况下它会产生您遇到的重复声明问题。

您最好的选择可能是定义 the _POSIX_C_SOURCE macro当你编译。 GLIBC 支持的值记录在链接的手册页上。任何值(可能 0 除外)都应该可以为您解决问题,但效果会更广泛,因此您可能需要尝试不同的值。首先,我建议值 200809L,这是 GLIBC 支持的值中最具包容性的值:

gcc -D_POSIX_C_SOURCE=200809L -std=c99 -Wall -O0 -ggdb -o sender sender.c spsc_circular_queue.c -lopus -lasound -lpthread

ALSA 应该依赖系统的定义,而不是发布自己的、重复的定义。

关于c - 在几个 header 中重新定义 'struct timeval',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43255796/

相关文章:

linux - 在 ALSA MIDI SysEx 交换上双向嗅探/监听

C++类重定义错误

objective-c - 错误 : redefinition of ‘struct StructName’ message when compiling in Objective-C on Linux

c - K叉树(排序词)

c - 根据 PID 杀死一个进程

c - Alsa,无法连续播放同一轨道两次

ffmpeg - ffmpeg 捕获中低质量源引起的 ALSA 缓冲区 xrun

在 c 中将字符数组转换为 uint32_t 数组——这是正确的方法吗?

用于传递多个可变参数的 C 宏

c - c中的重定义错误