C 编译器未加载的常量
我开始研究POSIX定时器,所以我也开始做一些练习,但我立即发现编译器有一些问题。 在编译这段代码时,我收到一些关于宏的奇怪消息,比如CLOCK_单调。这些都是在不同的库中定义的,比如time.h等,但是编译器给我的错误就好像它们没有定义一样 这很奇怪,因为我使用的是Fedora16,我的一些Ubuntu朋友得到的编译器错误比我少:-O 我是用gcc-O0-g3-Wall-c-fmessage length=0-std=c99-lrt编译的 下面是我得到的错误:C 编译器未加载的常量,c,posix,time.h,C,Posix,Time.h,我开始研究POSIX定时器,所以我也开始做一些练习,但我立即发现编译器有一些问题。 在编译这段代码时,我收到一些关于宏的奇怪消息,比如CLOCK_单调。这些都是在不同的库中定义的,比如time.h等,但是编译器给我的错误就好像它们没有定义一样 这很奇怪,因为我使用的是Fedora16,我的一些Ubuntu朋友得到的编译器错误比我少:-O 我是用gcc-O0-g3-Wall-c-fmessage length=0-std=c99-lrt编译的 下面是我得到的错误: struct-sigfeven
给出了:struct-sigfevent-sigfeventstruct
storage size of ‘sigeventStruct’ isn’t known unused variable ‘sigeventStruct’ [-Wunused-variable] Type 'sigevent' could not be resolved unknown type name ‘sigevent’
‘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
implicit declaration of function ‘timer_create’ [-Wimplicit-function- declaration] ‘CLOCK_MONOTONIC’ undeclared (first use in this function) Symbol 'CLOCK_MONOTONIC' could not be resolved
:sigeventStruct.sigev\u notify=sigev\u信号给出
storage size of ‘sigeventStruct’ isn’t known unused variable ‘sigeventStruct’ [-Wunused-variable] Type 'sigevent' could not be resolved unknown type name ‘sigevent’
‘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
implicit declaration of function ‘timer_create’ [-Wimplicit-function- declaration] ‘CLOCK_MONOTONIC’ undeclared (first use in this function) Symbol 'CLOCK_MONOTONIC' could not be resolved
给出了以下结果:if(timer\u create(CLOCK\u MONOTONIC,sigeventStruct,numero1)=-1)
storage size of ‘sigeventStruct’ isn’t known unused variable ‘sigeventStruct’ [-Wunused-variable] Type 'sigevent' could not be resolved unknown type name ‘sigevent’
‘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
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 <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
计时器编号1;
结构sigevent sigeventStruct;
sigeventStruct.sigev_notify=sigev_信号;
if(计时器创建(时钟单调,sigeventStruct,numero1)=-1)
{
printf(“错误:%s\n”,strerror(errno));
}
返回0;
}
如果您正在使用-std=c99
您需要告诉gcc您仍然在使用最新版本的POSIX:
#define _POSIX_C_SOURCE 199309L
在任何之前#包括
,甚至在命令行上使用-D
其他错误:
- 缺少
#包括
- 您需要一个用于创建
计时器的指针,即
,而不仅仅是&sigeventStruct
sigeventStruct
SIGEV\u信号、sigeventStruct
和CLOCK\u单调可用,您可以使用-std=gnu99
而不是-std=c99
编译代码
如@adwoodland所述,当\u POSIX\u C\u SOURCE
设置为值>=199309L
时,这些标识符被声明,这是-std=gnu99
的情况。您还可以使用-D_POSIX_C_SOURCE=199309L-std=c99
或在源代码中定义宏
其次,请参见timer\u create
prototype,您必须将指针作为第二个和第三个参数传递给函数:
timer_create(CLOCK_MONOTONIC, &sigeventStruct, &numero1)
^ ^
您还必须为strerror
函数声明包含标准标题string.h
。其他答案建议将\u POSIX\u C\u SOURCE
作为启用宏。这当然是可行的,但它不一定支持单一Unix规范(SUS)中的所有内容。为此,您应该设置,这也会自动设置\u POSIX\u C\u 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。它在一个头文件中,所以当我想要更改规则时,我只需要更改一个文件。我用-std=gnu99解决了很多问题(没有指定任何POSIX版本),但我仍然有
CLOCK_MONOTONIC could not be resolved
时钟的单调性无法解决
在互联网上搜索时,我发现一些Eclipse错误报告,人们对此表示不满。必须更好地检查是否存在Eclipse错误,因为
gcc -Wall -w -o Blala timer.c -std=gnu99 -lrt
gcc-Wall-w-o Blala定时器.c-std=gnu99-lrt
它针对未定义的时钟单调性问题编译:
正如Caterpillar指出的,这是一个eclipse错误,更准确地说是一个CDT索引器错误,其解决方法是只是一个建议,而不是以点形式出现错误,只需将它们完全复制到问题中的
块中即可。好的,下次我将考虑您的建议计时器也可以作为指针。我错过了,因为-Wall-Wextra
甚至没有对此发出警告!如果我使用-std=gnu99编译,我会有点困惑。我仍然需要在代码列表中编写#define(定义)POSIX(C)SOURCE 199309L?当使用-std=c99与使用-D_POSIX_C_SOURCE=199309L-std=c99或-std=c99和#define _POSIX_C_SOURCE 199309L?Caterpillar时,如果使用-std=gnu99
则无事可做,\u POSIX_C_SOURCE
将自动定义,且值将为>199309L
。对于-std=c99
,使用-D
或#define
解决方案是等效的。当使用#define
时,只需确保将#define
放在time.h
和signal.h
之前的一行中。我解决了很多问题,但仍然存在无法解决的时钟单调问题。我正在使用-std=gnu99谢谢,我保留这个库是为了进一步的目的,因为它是Eclipse的CDT插件的一个bug。如果链接死了,您应该添加解决方法