编译时遇到“has incomplete type” 的Error ,这种error 是在引用struct timespec 时还没有定义,因此,需要加入struct timespec 的定义。
In file included from compress.c:74:
/opt/compiler_tool/arm/gcc-linaro-9.2.1-20220411-x86_64_arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/libc/usr/include/sound/asound.h:458:18: error: field 鈥[01mtrigger_tstamp鈥has incomplete type
458 | struct timespec trigger_tstamp; /* time when stream was started/stopped/paused */
| ^~~~~~~~~~~~~~
/opt/compiler_tool/arm/gcc-linaro-9.2.1-20220411-x86_64_arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/libc/usr/include/sound/asound.h:459:18: error: field 鈥[01mtstamp鈥has incomplete type
459 | struct timespec tstamp; /* reference timestamp */
| ^~~~~~
/opt/compiler_tool/arm/gcc-linaro-9.2.1-20220411-x86_64_arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/libc/usr/include/sound/asound.h:468:18: error: field 鈥[01maudio_tstamp鈥has incomplete type
468 | struct timespec audio_tstamp; /* sample counter, wall clock, PHC or on-demand sync'ed */
| ^~~~~~~~~~~~
/opt/compiler_tool/arm/gcc-linaro-9.2.1-20220411-x86_64_arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/libc/usr/include/sound/asound.h:469:18: error: field 鈥[01mdriver_tstamp鈥has incomplete type
469 | struct timespec driver_tstamp; /* useful in case reference system tstamp is reported with delay */
| ^~~~~~~~~~~~~
/opt/compiler_tool/arm/gcc-linaro-9.2.1-20220411-x86_64_arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/libc/usr/include/sound/asound.h:471:37: error: invalid application of 鈥[01msizeof鈥to incomplete type 鈥[01mstruct timespec鈥
471 | unsigned char reserved[52-2*sizeof(struct timespec)]; /* must be filled with zero */
| ^~~~~~
/opt/compiler_tool/arm/gcc-linaro-9.2.1-20220411-x86_64_arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/libc/usr/include/sound/asound.h:478:18: error: field 鈥[01mtstamp鈥has incomplete type
478 | struct timespec tstamp; /* Timestamp */
| ^~~~~~
/opt/compiler_tool/arm/gcc-linaro-9.2.1-20220411-x86_64_arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/libc/usr/include/sound/asound.h:480:18: error: field 鈥[01maudio_tstamp鈥has incomplete type
480 | struct timespec audio_tstamp; /* from sample counter or wall clock */
参考这个链接,讲的比较清楚。
c - GCC with -std=c99 complains about not knowing struct timespec - Stack Overflow
struct timespec 在POSIX 中定义,我原始的makefile 为
CFLAGS += -std=c99 -Wall -Wextra -Wunused -DVERSION=\"${VER}\" -I./include
c99 标准没有定义struct timespec,因此会报error.
根据上述链接,需要Enable POSIX.
方式有3种,
1)CFLAGS 中,c99 改为gnu99, 验证可成功编译。
2)Adding -D_GNU_SOURCE to your CFLAGS will also work.验证可成功编译。
3)source code 中Enable Macro, 验证也可行。因为这种方法需要改source code ,而且我的报错是在/opt/compiler_tool/arm/gcc-linaro-9.2.1-20220411-x86_64_arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/libc/usr/include/sound/asound.h, 因此每个include asound.h 的地方,都需要加下面的这段code,这种方法不是很方便。
#if !defined(_XOPEN_SOURCE) && !defined(_POSIX_C_SOURCE) #if defined(__cplusplus) #define _XOPEN_SOURCE 700 /* SUS v4, POSIX 1003.1 2008/13 (POSIX 2008/13) */ #elif __STDC_VERSION__ >= 199901L #define _XOPEN_SOURCE 700 /* SUS v4, POSIX 1003.1 2008/13 (POSIX 2008/13) */ #else #define _XOPEN_SOURCE 500 /* SUS v2, POSIX 1003.1 1997 */ #endif /* __STDC_VERSION__ */ #endif /* !_XOPEN_SOURCE && !_POSIX_C_SOURCE */
针对这种方法,也可以在 CFLAGS 加固定的宏定义,比如-D_XOPEN_SOURCE=700
CFLAGS += -std=c99 -D_XOPEN_SOURCE=700 -Wall -Wextra -Wunused -DVERSION=\"${VER}\" -I./include
这种修改方法,可以成功编译。
更多编译宏,可以参考GNU manual description.