1

我在编译 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,没有任何反应

4

1 回答 1

4

ALSA 取决于类型struct timespecstruct timeval. 因此,它的global.h标题适当地做到了这一点:

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

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

#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 不同步,如果它确实一开始就完美同步,并且在某些情况下它会产生您遇到的重复声明问题。

您最好的选择可能是_POSIX_C_SOURCE编译时定义宏。GLIBC 支持的值记录在链接的手册页上。任何值(可能为 0 除外)都应该可以为您解决问题,但效果会更广泛,因此您可能需要尝试不同的值。首先,我建议使用 value 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 应该依赖系统的定义,而不是发布它自己的、重复的定义。

于 2017-04-06T13:26:29.587 回答