clock_gettime 和 CLOCK_MONOTONIC 上的编译错误
compilation error on clock_gettime and CLOCK_MONOTONIC
我正在程序中使用 clock_gettime。我试过包括 as 以及但都没有用。我还在我的编译器参数中添加了 -lrt,但我仍然遇到相同的错误。
这是在
CentOS Linux release 7.2.1511 (Core)
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-4)
GNU ld version 2.23.52.0.1-55.el7 20130226
ldd (GNU libc) 2.17
编译器输出:
gcc -o main packet.c connect.c transport.c accept.c main.c close.c util.c receive.c send.c congestion.c -Wall -g -std=c99 -lrt
util.c: In function ‘millis’:
util.c:42:21: error: storage size of ‘t’ isn’t known
struct timespec t;
^
util.c:43:5: warning: implicit declaration of function ‘clock_gettime’ [-Wimplicit-function-declaration]
clock_gettime(CLOCK_MONOTONIC_RAW, &t);
^
util.c:43:19: error: ‘CLOCK_MONOTONIC_RAW’ undeclared (first use in this function)
clock_gettime(CLOCK_MONOTONIC_RAW, &t);
^
生成文件
CFLAGS = -Wall -g -std=c99
LIBS = -lrt
# Should be equivalent to your list of C files, if you don't build selectively
SRC=$(wildcard *.c)
main: $(SRC)
gcc -o $@ $^ $(CFLAGS) $(LIBS)
util.h
的顶部
#ifndef UTILS_438_H_
#define UTILS_438_H_
#include "const.h"
#include "data.h"
#include "transport.h"
#include <time.h>
util.c
的顶部
#include "util.h"
#include <time.h>
#include <stdio.h>
#include <string.h>
如果我可以提供更多信息来帮助回答这个问题,请告诉我
在包含 header(<time.h>
) 之前,执行
#define _POSIX_C_SOURCE 199309L
http://man7.org/linux/man-pages/man2/clock_gettime.2.html
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
clock_getres(), clock_gettime(), clock_settime():
_POSIX_C_SOURCE >= 199309L
http://man7.org/linux/man-pages/man7/feature_test_macros.7.html
_POSIX_C_SOURCE
Defining this macro causes header files to expose definitions as
follows:
· The value 1 exposes definitions conforming to POSIX.1-1990 and
ISO C (1990).
· The value 2 or greater additionally exposes definitions for
POSIX.2-1992.
· The value 199309L or greater additionally exposes definitions
for POSIX.1b (real-time extensions).
· The value 199506L or greater additionally exposes definitions
for POSIX.1c (threads).
· (Since glibc 2.3.3) The value 200112L or greater additionally
exposes definitions corresponding to the POSIX.1-2001 base
specification (excluding the XSI extension). This value also
causes C95 (since glibc 2.12) and C99 (since glibc 2.10)
features to be exposed (in other words, the equivalent of
defining _ISOC99_SOURCE).
· (Since glibc 2.10) The value 200809L or greater additionally
exposes definitions corresponding to the POSIX.1-2008 base
specification (excluding the XSI extension).
如果您可以使用以下链接器和编译器标志
CFLAGS = -g -Wall -std=c99 -D_POSIX_SOURCE -D_GNU_SOURCE
LDFLAGS = -lpthread -lrt
您可以在 Linux 系统中完成构建。 -lrt 链接器和 -D_POSIX_SOURCE 是重要的
只需将 -std=c99 替换为 -std=gnu99。
这样你就不必添加 _POSIX_C_SOURCE
我正在程序中使用 clock_gettime。我试过包括 as 以及但都没有用。我还在我的编译器参数中添加了 -lrt,但我仍然遇到相同的错误。
这是在
CentOS Linux release 7.2.1511 (Core)
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-4)
GNU ld version 2.23.52.0.1-55.el7 20130226
ldd (GNU libc) 2.17
编译器输出:
gcc -o main packet.c connect.c transport.c accept.c main.c close.c util.c receive.c send.c congestion.c -Wall -g -std=c99 -lrt
util.c: In function ‘millis’:
util.c:42:21: error: storage size of ‘t’ isn’t known
struct timespec t;
^
util.c:43:5: warning: implicit declaration of function ‘clock_gettime’ [-Wimplicit-function-declaration]
clock_gettime(CLOCK_MONOTONIC_RAW, &t);
^
util.c:43:19: error: ‘CLOCK_MONOTONIC_RAW’ undeclared (first use in this function)
clock_gettime(CLOCK_MONOTONIC_RAW, &t);
^
生成文件
CFLAGS = -Wall -g -std=c99
LIBS = -lrt
# Should be equivalent to your list of C files, if you don't build selectively
SRC=$(wildcard *.c)
main: $(SRC)
gcc -o $@ $^ $(CFLAGS) $(LIBS)
util.h
的顶部#ifndef UTILS_438_H_
#define UTILS_438_H_
#include "const.h"
#include "data.h"
#include "transport.h"
#include <time.h>
util.c
的顶部#include "util.h"
#include <time.h>
#include <stdio.h>
#include <string.h>
如果我可以提供更多信息来帮助回答这个问题,请告诉我
在包含 header(<time.h>
) 之前,执行
#define _POSIX_C_SOURCE 199309L
http://man7.org/linux/man-pages/man2/clock_gettime.2.html
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
clock_getres(), clock_gettime(), clock_settime(): _POSIX_C_SOURCE >= 199309L
http://man7.org/linux/man-pages/man7/feature_test_macros.7.html
_POSIX_C_SOURCE Defining this macro causes header files to expose definitions as follows:
· The value 1 exposes definitions conforming to POSIX.1-1990 and ISO C (1990). · The value 2 or greater additionally exposes definitions for POSIX.2-1992. · The value 199309L or greater additionally exposes definitions for POSIX.1b (real-time extensions). · The value 199506L or greater additionally exposes definitions for POSIX.1c (threads). · (Since glibc 2.3.3) The value 200112L or greater additionally exposes definitions corresponding to the POSIX.1-2001 base specification (excluding the XSI extension). This value also causes C95 (since glibc 2.12) and C99 (since glibc 2.10) features to be exposed (in other words, the equivalent of defining _ISOC99_SOURCE). · (Since glibc 2.10) The value 200809L or greater additionally exposes definitions corresponding to the POSIX.1-2008 base specification (excluding the XSI extension).
如果您可以使用以下链接器和编译器标志
CFLAGS = -g -Wall -std=c99 -D_POSIX_SOURCE -D_GNU_SOURCE
LDFLAGS = -lpthread -lrt
您可以在 Linux 系统中完成构建。 -lrt 链接器和 -D_POSIX_SOURCE 是重要的
只需将 -std=c99 替换为 -std=gnu99。 这样你就不必添加 _POSIX_C_SOURCE