常量未由编译器加载

Ger*_*llo 9 c posix time.h

我开始学习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’
    
    Run Code Online (Sandbox Code Playgroud)
  • 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
    
    Run Code Online (Sandbox Code Playgroud)
  • 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
    
    Run Code Online (Sandbox Code Playgroud)

这是代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

oua*_*uah 13

首先,你可以编译代码-std=gnu99,而不是-std=c99如果你想有标识符SIGEV_SIGNAL, sigeventStructCLOCK_MONOTONIC可用.

正如@adwoodland所指出的那样,这些标识符在_POSIX_C_SOURCE设置为值> = 时被声明199309L,就是这种情况-std=gnu99.您还可以使用-D_POSIX_C_SOURCE=199309L -std=c99或拥有源代码中定义的宏.

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

timer_create(CLOCK_MONOTONIC, &sigeventStruct, &numero1)
                              ^                ^
Run Code Online (Sandbox Code Playgroud)

你也需要包含标准头string.hstrerror函数声明.


Fle*_*exo 6

如果您正在使用-std=c99,则需要告诉gcc您仍在使用最新版本的POSIX:

#define _POSIX_C_SOURCE 199309L
Run Code Online (Sandbox Code Playgroud)

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

其他错误:

  • 失踪 #include <string.h>
  • 你需要一个指针timer_create,&sigeventStruct而不仅仅是sigeventStruct