9

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

5 回答 5

14

首先,如果您想要标识符、 和可用,您可以使用-std=gnu99而不是编译您的代码。-std=c99SIGEV_SIGNALsigeventStructCLOCK_MONOTONIC

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

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

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

您还必须包含函数声明的标准标题string.hstrerror

于 2012-01-16T14:54:04.410 回答
8

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

#define _POSIX_C_SOURCE 199309L

any之前#include,甚至-D在命令行上。

其他错误:

  • 失踪#include <string.h>
  • 你需要一个指针timer_create,即,&sigeventStruct而不仅仅是sigeventStruct
于 2012-01-16T14:52:03.193 回答
4

其他答案建议_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。它位于标头中,因此当我想更改规则时只需更改一个文件。

于 2012-01-16T15:27:20.430 回答
2

提到 CLOCK_MONOTONIC 未定义问题:

正如 Caterpillar 指出的那样,这是一个 eclipse 错误,更准确地说是一个 CDT-Indexer 错误,在eclipse 错误处有一个解决方法,评论 12

于 2012-05-02T11:20:26.027 回答
1

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

CLOCK_MONOTONIC 无法解析

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

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

它编译

于 2012-01-17T13:09:45.863 回答