6

I'm using clock_gettime in a program. I've tried including as well as but neither works. I have also added -lrt to my compiler arguments but still I get the same errors.

This is on

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

Compiler output:

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

Makefile

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)

Top of util.h

#ifndef UTILS_438_H_
#define UTILS_438_H_

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

#include <time.h>

Top of util.c

#include "util.h"

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

Please let me know if I can supply more information to help answer this

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Steven
  • 285
  • 1
  • 3
  • 11
  • 1
    that's not a linker error,, `#include` problem somewhere – yano Nov 09 '16 at 20:37
  • 2
    you should figure out why you're getting the compiler warnings and errors. It's _not_ a link problem. Since you're including `time.h` you shouldn't have any warnings. Can you create a [mcve] so we can help you further? Maybe the `c99` flag gets in the way? (just trying to guess something) – Jean-François Fabre Nov 09 '16 at 20:38
  • 2
    will you try -std=gnu99 instead? – Bonan Nov 09 '16 at 20:40
  • Also, linking order can and does matter. Surely someone here can explain in more detail, but I'd be wary of putting `-lrt` at the very end of my `gcc` command. I suspect you may run into "undefined reference" problems with that at the end after you resolve this issue. – yano Nov 09 '16 at 20:44
  • Replacing -std=c99 with -std=gnu99 allowed it to compile. Removing the -std also compiled fine. What is wrong with using c99? – Steven Nov 09 '16 at 21:07
  • 2
    @Steven When you use -std=c99, you only get access to the standard C functions in the standard header files. clock_gettime() from time.h is defined by the posix standard, not the C standard. – nos Nov 09 '16 at 21:35

3 Answers3

11

Before including the header(<time.h>), do

#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).
user3528438
  • 2,737
  • 2
  • 23
  • 42
  • 1
    tried that on MinGW, still the same compilation errors... You read this: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47571 – Jean-François Fabre Nov 09 '16 at 20:46
  • 1
    @Jean-FrançoisFabre Mingw does not give you access to all posix functions. The OP is using Linux, so this will work fine. – nos Nov 09 '16 at 21:34
4

Simply replace -std=c99 with -std=gnu99. Thisway you do not have to add _POSIX_C_SOURCE

JimBamFeng
  • 709
  • 1
  • 4
  • 20
0

if you can use the following linker and Compiler flags

CFLAGS  = -g -Wall -std=c99 -D_POSIX_SOURCE -D_GNU_SOURCE
LDFLAGS = -lpthread -lrt 

you can get the build done in the Linux systems. -lrt linker and -D_POSIX_SOURCE are the important ones

  • 1
    This answer is not helping at all. Please, provide further links to your statements and give appropriate solution. The way you responded does not solve what OP asked. – Harald Mar 24 '20 at 09:55