C++ Error: Undefined Reference to 'Clock_Gettime' and 'Clock_Settime'

C++ error: undefined reference to 'clock_gettime' and 'clock_settime'

Add -lrt to the end of g++ command line. This links in the librt.so "Real Time" shared library.

Undefined reference to `clock_gettime', modify makefile?

ipopt i see is configured with configure script, reading help from that script i guess you can just ADD_CFLAGS compiler flags:

ADD_CFLAGS=-lrt ./configure

and then run make.

Undefined reference to `clock_gettime` although `-lrt` is given

Well the problem is solved If i pass this linker flag

-Wl,--no-as-needed

Before the library list in command line.

Why this works because in my platform, linker is always passed with -Wl,--as-needed.

From ld manual :

--as-needed
--no-as-needed
This option affects ELF DT_NEEDED tags for dynamic libraries
mentioned on the command line after the --as-needed option.
Normally the linker will add a DT_NEEDED tag for each dynamic
library mentioned on the command line, regardless of whether the
library is actually needed or not. --as-needed causes a DT_NEEDED
tag to only be emitted for a library that satisfies an undefined
symbol reference from a regular object file or, if the library is
not found in the DT_NEEDED lists of other libraries linked up to
that point, an undefined symbol reference from another dynamic
library. --no-as-needed restores the default behaviour.

So when --as-needed is given before a library , liker only links with the libraries which are given in NEEDED section of the library.

For example,

-Wl,--as-needed -llibA -llibB -llibC

Here --as-needed is given before libA. So during linking, linker will examine the NEEDED section of libA. If in NEEDED section of libA lists only libC, then the libB will not be linked.

This specific problem occurred because

arif@khost:~/sak/sak.exosip$ objdump -p /opt/osip2/lib/libosip2.so.10 | grep NEEDED
NEEDED libosipparser2.so.10
NEEDED libc.so.6

libosip2 does not lists librt as NEEDED.

If i pass --no-as-needed, then all the libraries will be linked regardless of what is given in ELF's NEEDED section.

Although this should not be the case because,

arif@khost:~/sak/sak.exosip$ nm --demangle /opt/osip2/lib/libosip2.so.10 | grep clock_gettime
U clock_gettime

It has undefined symbol clock_gettime which is provided by librt.so.

Well its actually a fault of the libosip2 devs that their autotools is not working with --as-needed.

The link command used by osip:

libtool: link: gcc -shared  -fPIC -DPIC  .libs/ict_fsm.o .libs/ist_fsm.o .libs/nict_fsm.o .libs/nist_fsm.o .libs/ict.o .libs/ist.o .libs/nict.o .libs/nist.o .libs/fsm_misc.o .libs/osip.o .libs/osip_transaction.o .libs/osip_event.o .libs/port_fifo.o .libs/osip_dialog.o .libs/osip_time.o .libs/port_sema.o .libs/port_thread.o .libs/port_condv.o   -Wl,-rpath -Wl,/home/arif/sak/osip/src/osipparser2/.libs -Wl,-rpath -Wl,/opt/osip2-test/lib -lnsl ../osipparser2/.libs/libosipparser2.so    -Wl,-soname -Wl,libosip2.so.10 -o .libs/libosip2.so.10.0.0

So its not linking with librt and thats why its not listing librt in its NEEDED list

If configured with :

 LDFLAGS="${LDFLAGS} -lrt" ./configure --prefix=/opt/osip2-test/ 

Then the link command becomes :

libtool: link: gcc -shared  -fPIC -DPIC  .libs/ict_fsm.o .libs/ist_fsm.o .libs/nict_fsm.o .libs/nist_fsm.o .libs/ict.o .libs/ist.o .libs/nict.o .libs/nist.o .libs/fsm_misc.o .libs/osip.o .libs/osip_transaction.o .libs/osip_event.o .libs/port_fifo.o .libs/osip_dialog.o .libs/osip_time.o .libs/port_sema.o .libs/port_thread.o .libs/port_condv.o   -Wl,-rpath -Wl,/home/arif/sak/osip/src/osipparser2/.libs -Wl,-rpath -Wl,/opt/osip2-test/lib -lnsl ../osipparser2/.libs/libosipparser2.so -lrt    -Wl,-soname -Wl,libosip2.so.10 -o .libs/libosip2.so.10.0.0

So its linking with librt. Its also reflected in its ELF:

arif@khost:~/sak/osip/src/osip2/.libs$ objdump -p libosip2.so.10 | grep NEEDED
NEEDED libosipparser2.so.10
NEEDED librt.so.1
NEEDED libc.so.6

This patch fixes this :

diff --git a/src/osip2/Makefile.am b/src/osip2/Makefile.am
index bb0d8f3..b72c22a 100644
--- a/src/osip2/Makefile.am
+++ b/src/osip2/Makefile.am
@@ -14,7 +14,7 @@ libosip2_la_SOURCES+=port_sema.c port_thread.c port_condv.c
endif

libosip2_la_LDFLAGS = -version-info $(LIBOSIP_SO_VERSION) \
- $(FSM_LIB) $(EXTRA_LIB) ../osipparser2/libosipparser2.la -no-undefined
+ $(FSM_LIB) $(EXTRA_LIB) ../osipparser2/libosipparser2.la -no-undefined -lrt

INCLUDES = -I$(top_srcdir)/includ

Relevant usenet discussion thread :
https://groups.google.com/forum/#!topic/comp.unix.programmer/VKbARy6W4AY

UPDATE:

osip developer responded to my mail. He fixed it with a different patch (More general solution then mine)
http://git.savannah.gnu.org/cgit/osip.git/commit/?id=bd5b1ad58381e4bfce08bad9b66ad00cd28f9b65

Undefined reference to clock_gettime() in Linux using ICC

It looks like you haven't linked librt.a at all since the linker is ignoring -l. Perhaps you were supposed to use -lrt and optionally give the path via -L.

icpc  -lrt -L/usr/lib/x86_64-linux-gnu -o "test"  ./src/test.o

Notice I have no spaces between the -l and its parameter. I also have listed "librt.a" as merely rt; the linker will add the rest on its own.

Undefined reference to clock_gettime, gcc 4.6 & cmake

You should ensure that -lpthread -lrt appear after your libraries like ../lib/libLibUtil.a

compilation error on clock_gettime and CLOCK_MONOTONIC

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).


Related Topics



Leave a reply



Submit