2016-11-09 1 views
1

프로그램에서 clock_gettime을 사용하고 있습니다. 나는뿐만 아니라 둘 다 시도했지만 시도하지 못했습니다. 또한 컴파일러 인수에 -lrt를 추가했지만 여전히 동일한 오류가 발생합니다. clock_gettime 및 CLOCK_MONOTONIC의 컴파일 오류

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 

컴파일러 출력에 : util.h의

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 [email protected] $^ $(CFLAGS) $(LIBS) 

최고

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); 
      ^

메이크

#ifndef UTILS_438_H_ 
#define UTILS_438_H_ 

#include "const.h" 
#include "data.h" 
#include "transport.h" 

#include <time.h> 
,536,913,632 util.c 10

최고

#include "util.h" 

#include <time.h> 
#include <stdio.h> 
#include <string.h> 

, 나는 헤더 (<time.h>)를 포함하기 전에이

+1

링커 오류가 아니며, '# include' 문제가있는 곳 – yano

+2

컴파일러 경고 및 오류가 발생하는 이유를 알아야합니다. 그것은 링크 문제가 아닙니다. 'time.h'을 포함하고 있기 때문에 경고를해서는 안됩니다. 우리가 당신을 더 도울 수 있도록 [mcve]를 만들 수 있습니까? 어쩌면'c99' 플래그가 방해가 될까요? (그냥 뭔가를 추측하려 함) –

+2

-std = gnu99를 대신 사용 하시겠습니까? – Bonan

답변

5

에 답 더 많은 정보를 제공 할 수 있는지 알려

#define _POSIX_C_SOURCE 199309L 

을주세요

http://man7.org/linux/man-pages/man2/clock_gettime.2.html

glibc에 대한 63,210

기능 테스트 매크로 요구 사항 (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 이 다음과 같이이 매크로 원인은 정의를 노출하는 헤더 파일 정의 :

· 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). 
+0

MinGW에서 여전히 동일한 컴파일 오류가 발생했습니다 ...이 내용을 읽으십시오 : https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47571 –

+0

@ Jean-FrançoisFabre Mingw는 posix 함수. OP는 Linux를 사용하기 때문에 정상적으로 작동합니다. – nos

+0

gcc 버전이 5.4이고, glibc 버전이 2.23이지만, 이미 정의되어 있습니다. 나는 이전의 ldd와 gcc 버전 (4.8.4)에서 그것을 필요로했다. –