常量未由编译器加载

我开始学习POSIX计时器,所以我开始做一些练习,但我立即遇到了编译器的一些问题。 在编译这段代码时,我收到一些关于像CLOCK_MONOTONIC这样的宏的奇怪消息。 这些在各种库中定义,如time.h等,但编译器给出了错误,好像它们没有定义。

这很奇怪,因为我使用的是Fedora 16,而我的一些Ubuntu朋友得到的编译错误比我少:-O

我正在使用gcc -O0 -g3 -Wall -c -fmessage-length=0 -std=c99 -lrt

我得到的错误:

  • struct sigevent sigeventStruct 给出

     storage size of 'sigeventStruct' isn't known unused variable 'sigeventStruct' [-Wunused-variable] Type 'sigevent' could not be resolved unknown type name 'sigevent' 
  • sigeventStruct.sigev_notify = SIGEV_SIGNAL 给出

     'SIGEV_SIGNAL' undeclared (first use in this function) request for member 'sigev_notify' in something not a structure or union Field 'sigev_notify' could not be resolved 
  • if(timer_create(CLOCK_MONOTONIC, sigeventStruct, numero1) == -1) 给出

     implicit declaration of function 'timer_create' [-Wimplicit-function- declaration] 'CLOCK_MONOTONIC' undeclared (first use in this function) Symbol 'CLOCK_MONOTONIC' could not be resolved 

这是代码:

 #include  #include  #include  #include  #include  #include  #include  int main() { timer_t numero1; struct sigevent sigeventStruct; sigeventStruct.sigev_notify = SIGEV_SIGNAL; if(timer_create(CLOCK_MONOTONIC, sigeventStruct, numero1) == -1) { printf( "Errore: %s\n", strerror( errno ) ); } return 0; } 

首先,如果要使用标识符SIGEV_SIGNALsigeventStructCLOCK_MONOTONIC可以使用-std=gnu99而不是-std=c99编译代码。

如@adwoodland所述,当_POSIX_C_SOURCE设置为值> = 199309L ,将声明这些标识符,这是-std=gnu99的情况。 您也可以使用-D_POSIX_C_SOURCE=199309L -std=c99或在源代码中定义宏。

其次,请参阅timer_create原型,您必须将指针作为函数的第二个和第三个参数传递:

 timer_create(CLOCK_MONOTONIC, &sigeventStruct, &numero1) ^ ^ 

此外,您还必须包含用于strerror函数声明的标准头string.h .

如果你使用-std=c99你需要告诉gcc你还在使用最新版本的POSIX:

 #define _POSIX_C_SOURCE 199309L 

任何#include 之前 ,甚至在命令行上使用-D

其他错误:

  • 缺少#include
  • 你需要一个指针用于timer_create ,即&sigeventStruct而不仅仅是sigeventStruct

其他答案建议将_POSIX_C_SOURCE作为启用宏。 这肯定有效,但它并不一定能实现单Unix规范(SUS)中的所有内容。 为此,您应该设置_XOPEN_SOURCE ,它也会自动设置_POSIX_C_SOURCE 。 我有一个标题,我称之为"posixver.h" ,其中包含:

 /* ** Include this file before including system headers. By default, with ** C99 support from the compiler, it requests POSIX 2001 support. With ** C89 support only, it requests POSIX 1997 support. Override the ** default behaviour by setting either _XOPEN_SOURCE or _POSIX_C_SOURCE. */ /* _XOPEN_SOURCE 700 is loosely equivalent to _POSIX_C_SOURCE 200809L */ /* _XOPEN_SOURCE 600 is loosely equivalent to _POSIX_C_SOURCE 200112L */ /* _XOPEN_SOURCE 500 is loosely equivalent to _POSIX_C_SOURCE 199506L */ #if !defined(_XOPEN_SOURCE) && !defined(_POSIX_C_SOURCE) #if __STDC_VERSION__ >= 199901L #define _XOPEN_SOURCE 600 /* SUS v3, POSIX 1003.1 2004 (POSIX 2001 + Corrigenda) */ #else #define _XOPEN_SOURCE 500 /* SUS v2, POSIX 1003.1 1997 */ #endif /* __STDC_VERSION__ */ #endif /* !_XOPEN_SOURCE && !_POSIX_C_SOURCE */ 

它针对我使用的系统进行了调整,并不是所有人都能识别700值。 如果您正在使用相对现代的Linux,我相信您可以使用700.它位于标题中,因此当我想要更改规则时,我只需要更改一个文件。

参考CLOCK_MONOTONIC未定义的问题:

正如卡特彼勒所指出的那样,这是一个日食错误,更确切地说是一个CDT-Indexer错误,其中包含日食错误的解决方法,评论12

我用-std = gnu99解决了很多问题(没有指定任何POSIX版本),但我仍然有

  CLOCK_MONOTONIC无法解决 

在互联网上搜索我发现了一些Eclipse bug报告,人们抱怨这个。 必须更好地检查是否是Eclipse错误,因为有

  gcc -Wall -w -o Blala timer.c -std = gnu99 -lrt 

它汇编