2016-09-14 148 views
0

您好,并提前感谢此平台过去为我解决的所有问题。不幸的是我发现了一个我无法解决的问题。将C标准更改为c99后,使用UNIX time.h的错误

我对cmake非常陌生,并用新的可执行文件和一些库文件扩展了一个演示项目。我在编译演示项目时没有问题。但是,我的新项目需要使用c99标准进行编译,突然间,我得到了time.h中实现timepec结构的错误。这也是在演示项目中使用的,所以我使用c99重新编译了演示,并且遇到了同样的问题。

在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 

这是definining _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:使用C99的gnu99而不是对我的解决方案是。现在我可以用cmake创建UNIX Makefiles,但仍然无法创建正在运行的Eclipse项目。
由于这是一个不同的问题,我想这个案子是封闭的,谢谢大家的帮助和努力!

+0

请分享你的错误。 –

+0

对不起,没有想到这一点。将其添加到原始帖子中,以便更多人可以看到它。 – jckaos

+0

根本没有提及'cmake'的意思。输入完成后,只需将gcc报告的错误复制/粘贴到shell中:'make VERBOSE = 1' – malat

回答

0

作为man timeradd说,功能timeradd定义仅当_DEFAULT_SOURCE特征测试宏被定义:

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_SOURCEman feature_test_macros说:

可以使用这个宏定义为确保即使在默认值将被禁用时提供“默认”定义 , 也会发生单独的宏被明确定义,或者编译器以其“标准”模式之一调用(例如, cc -std = c99)。

所以,你需要在-std=c99模式明确地定义_DEFAULT_SOURCE宏化妆功能timeradd是可用:

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

感谢您的建议。我通过使用gnu99而不是c99解决了这个问题。现在一切正常,当我使用cmake生成UNIX Makefiles时。当我尝试生成一个Eclipse项目时,我仍然遇到了timespec无法解析的错误,但我猜这是一个不同的故事。 – jckaos