c - 将 C 标准更改为 c99 后使用 UNIX time.h 时出错

标签 c linux cmake

<分区>

您好,在此先感谢该平台过去为我解决的所有问题。不幸的是,我发现了一个我无法解决的问题。

我是 cmake 的新手,并使用新的可执行文件和一些库文件扩展了一个演示项目。我编译演示项目没有问题。但是,我的新项目需要使用 c99 标准进行编译,突然间,我在实现 time.h 的 timespec 结构时遇到错误。 demo工程中也用到了,所以我又用c99编译了demo,还是一样的问题。

在 Ubuntu 上运行,使用 gcc 编译器和 cmake 版本 2.8.7

希望我已经涵盖了所有必要的细节。如果没有,请让我知道并提前感谢您的努力!

最好的问候

编辑#1:我收到的错误消息:
->CLOCK_MONOTONIC< 未声明(在此函数中首次使用)
- 字段“tv_nsec”无法解析
- 无法解析字段“tv_sec”
- 无法解析符号“CLOCK_MONOTONIC”
- 隐式声明函数 'clock_gettime'、'nanosleep'、'timeradd'、'timercmp' 的警告

编辑#2:make VERBOSE=1 时的错误输出

/usr/bin/gcc  -D_XOPEN_SOURCE=600 -I/home/localadmin/Eclipse_Workspace/SOEM_master/soem -I/home/localadmin/Eclipse_Workspace/SOEM_master/osal -I/home/localadmin/Eclipse_Workspace/SOEM_master/osal/linux -I/home/localadmin/Eclipse_Workspace/SOEM_master/oshw/linux    -std=c99 -o CMakeFiles/soem.dir/osal/linux/osal.c.o   -c /home/localadmin/Eclipse_Workspace/SOEM_master/osal/linux/osal.c  
/home/localadmin/Eclipse_Workspace/SOEM_master/osal/linux/osal.c:60:50: Warning: »struct timezone« declared in parameter list [activated by default]  
/home/localadmin/Eclipse_Workspace/SOEM_master/osal/linux/osal.c:60:50: Warning: range of validity includes only this definition or declaration [activated by default]  
/home/localadmin/Eclipse_Workspace/SOEM_master/osal/linux/osal.c: In function »osal_timer_start«:  
/home/localadmin/Eclipse_Workspace/SOEM_master/osal/linux/osal.c:105:4: Warning: Implicit function »timeradd« [-Wimplicit-function-declaration]  
/home/localadmin/Eclipse_Workspace/SOEM_master/osal/linux/osal.c: In function »osal_timer_is_expired«:  
/home/localadmin/Eclipse_Workspace/SOEM_master/osal/linux/osal.c:120:4: Warning: Implicit declaration of function »timercmp« [-Wimplicit-function-declaration]  
/home/localadmin/Eclipse_Workspace/SOEM_master/osal/linux/osal.c:120:61: Error: expected expression before »<« token  
make[2]: *** [CMakeFiles/soem.dir/osal/linux/osal.c.o] Error 1  
make[2]: Leaving directory '/home/localadmin/Eclipse_Workspace/SOEM_master/build'  
make[1]: *** [CMakeFiles/soem.dir/all] Error 2  
make[1]: Leaving directory '/home/localadmin/Eclipse_Workspace/SOEM_master/build'  
make: *** [all] Error 2

这是定义 _XOPEN_SOURCE=600 后的输出,这是在下面发布的另一个线程中建议的。因此 timespec 结构可用,但函数不可用。

编辑 #3:最小、完整且可验证的示例

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
int main()
{
struct timespec test;
test.tv_sec = 0;
struct timeval start_time;
struct timeval timeout;
struct timeval stop_time;
timeradd(&start_time, &timeout, &stop_time);
return 0;
}  

编译没有问题。如果我使用 gcc mcv_example.c -std=c99 我得到:

mcv_example.c: In function 'main':
mcv_example.c:24:18: error: storage size of 'test' isn't known
mcv_example.c:29:2: warning: implicit declaration of function 'timeradd' [-Wimplicit-function-declaration]

编辑#4:我的解决方案是使用 gnu99 而不是 c99。现在我可以使用 cmake 创建 UNIX Makefile,但仍然无法创建一个有效的 Eclipse 项目。
由于这是一个不同的问题,我想这个案例已经结束,感谢大家的帮助和努力!

最佳答案

作为man timeradd说,只有在定义了 _DEFAULT_SOURCE feature-test-macro 时,函数 timeradd 的定义才可用:

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

   All functions shown above:
       Since glibc 2.19:
           _DEFAULT_SOURCE
       Glibc 2.19 and earlier:
           _BSD_SOURCE

_DEFAULT_SOURCE 宏的描述 man feature_test_macros说:

This macro can be defined to ensure that the "default" definitions are provided even when the defaults would otherwise be disabled, as happens when individual macros are explicitly defined, or the compiler is invoked in one of its "standard" modes (e.g., cc -std=c99).

因此您需要明确定义 _DEFAULT_SOURCE 宏以使函数 timeradd-std=c99 模式下可用:

#define _DEFAULT_SOURCE
#include <sys/time.h>
...
timeradd(...);

关于c - 将 C 标准更改为 c99 后使用 UNIX time.h 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39487193/

相关文章:

c - eCos : Compile and Run sample application on Linux

c - 如何同时使用多个while循环?

c++ - 总的通用指针类型是什么?

当我向 keyname() 发送大于 256 的 int 计数器变量时,Curses keyname() 会出现段错误

php - super 表单 WordPress 插件可以在不同的操作系统上使用 php 7.0 版本吗?

c++ - 使用 cmake 构建具有 "experimental/filesystem"的项目

cmake 设置子目录选项

visual-studio - C测试,VS2010。如何摆脱 Nightly、NightlyMemCheck 等?

linux - 我组中的用户无法在 python 中导入 boxsdk(SyntaxError : invalid syntax )

c++ - 从同一网络中另一台机器的哈希表获取 wordID