繁体   English   中英

Linux中timersub()函数的隐式声明-我必须定义什么?

[英]Implicit declaration of timersub() function in Linux - what must I define?

我正在尝试在linux中编译timersub()函数,但我总是得到:

test.c: In function ‘main’:
test.c:27:2: warning: implicit declaration of function ‘timersub’ [-Wimplicit-function-declaration]
  timersub(&now, &then, &diff);
  ^

/tmp/ccLzfLsl.o: In function `main':
test.c:(.text+0x55): undefined reference to `timersub'
collect2: error: ld returned 1 exit status

这只是我使用的所有库的简单代码。

#define _XOPEN_SOURCE
#define _POSIX_SOURCE
#include <stdio.h>

#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "openflow.h"
#include "cbench.h"
#include "fakeswitch.h"
#include <unistd.h>



int main()
{
    struct timeval now, then, diff;


    gettimeofday(&then,NULL);

    sleep(1);

    gettimeofday(&now, NULL);

    timersub(&now, &then, &diff);

return 0;


} 

我正在用它编译:

gcc --std = c99 -Wall -DTRACE -o test test.c

请参阅手册 它不在POSIX中,而在BSD功能中。 因此,您需要_BSD_SOURCE

在顶部定义它:

#define _BSD_SOURCE

或编译为:

gcc --std=c99 -Wall -DTRACE -D_BSD_SOURCE -o test test.c 

从Glibc 2.20开始,宏_BSD_SOURCE已被弃用,并已由_DEFAULT_SOURCE取代。 功能测试宏

_DEFAULT_SOURCE(自glibc 2.19起)

可以定义该宏以确保即使在默认情况下将禁用默认设置时也提供“默认”定义,如在显式定义单个宏或以其“标准”模式之一(例如,cc -std = C99)。 定义_DEFAULT_SOURCE而不定义其他单个宏或以其“标准”模式之一调用编译器无效。

“默认”定义包括POSIX.1-2008和ISO C99所要求的定义,以及最初源自BSD和System V的各种定义。在glibc 2.19及更早版本上,这些默认值近似等于显式定义以下内容:

cc -D_BSD_SOURCE -D_SVID_SOURCE -D_POSIX_C_SOURCE = 200809

但是,如果您使用的是较旧的Gblic,则仍然需要使用_BSD_SOURCE

基本上我只是删除了#define _XOPEN_SOURCE和#define _POSIX_SOURCE并仅使用gcc -Wall -DTRACE对其进行了编译,并且可以正常工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM