我开始研究 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 <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;
}