I have several issues to compile a C program to stream sound from Intel Edison to devices (iOS and Android).
I made a C program : I use alsa/asoundlib.h and pthread.h in my program I don't include sys/time.h because ALSA does not allow this.
I use lot of timeval in my program, when i compile it on my computer i'ts compile fine, but on my edison when I :
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
How can I manage to stop these redifinitions ?! Thank you for your help !
Extra info:
I include :
#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"
I remove sched.h, nothing happen
ALSA depends on types struct timespec
and struct timeval
. Its global.h
header therefore appropriately does this:
/* for timeval and timespec */ #include <time.h>
However, it seems to be of the opinion that GLIBC defines those structures only when an appropriate feature-test macro has been defined, for that header also says:
#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
It's hard to determine under what circumstances GLIBC actually does declare the wanted structures. It indeed does do so conditionally, but it appears that the conditions, at least in GLIBC v2.17, are more general than ALSA assumes. Thus ALSA seems to have fallen out of sync with GLIBC, if indeed it ever was perfectly synchronized in the first place, and under some conditions it produces the duplicate declaration problem you encountered.
Your best bet is probably to define the _POSIX_C_SOURCE
macro when you compile. The values supported by GLIBC are documented on the linked manual page. Any value, except possibly 0, ought to solve the problem for you, but the effects will be broader, so you may need to experiment with different values. To start, I suggest the value 200809L
, which is the most inclusive among the values supported by GLIBC:
gcc -D_POSIX_C_SOURCE=200809L -std=c99 -Wall -O0 -ggdb -o sender sender.c spsc_circular_queue.c -lopus -lasound -lpthread
ALSA should then rely on the system's definitions instead of issuing its own, duplicate ones.