What library does ld option -lrt refer to (Bionic libc)?
Asked Answered
C

3

45

What does this option to ld mean?

-lrt

I know to some degree it means that ld is looking for the library with real-time extensions, but after searching extensively, I couldn't find an exact definition (or which library) this is referring to.

With Bionic (android) libc, the android NDK docs detail that this (along with pthreads) has been integrated.

I ask because it seems to be implicit (ie when I invoke the compiler, a custom-compiled gdc, directly, I get a message saying that ld cannot find -lrt)

EDIT

From the Bionic Overview

Bionic's C library comes with its own pthread implementation bundled in. This is different from other historical C libraries which:

- place it in an external library (-lpthread)
- play linker tricks with weak symbols at dynamic link time

The support for real-time features (a.k.a. -lrt) is also bundled in the C library.

The problem is that collect2 is passing the option -lrt (and also -lpthreads) to ld.

dan@devbox:~/projects/gdc_test/jni/src$ $DC gdc_test.d -v -mthumb
Using built-in specs.
Target: arm-linux-androideabi
Configured with: /arm/ndk-git/src/build/../gcc/gcc-4.4.3/configure --prefix=/arm/ndk-git/ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86 --target=arm-linux-androideabi --host=x86_64-linux-gnu --build=x86_64-linux-gnu --with-gnu-as --with-gnu-ld --enable-languages=c,c++ --with-gmp=/arm/ndk-git/build_run/temp-install --with-mpfr=/arm/ndk-git/build_run/temp-install --disable-libssp --enable-threads --disable-nls --disable-libmudflap --disable-libgomp --disable-libstdc__-v3 --disable-sjlj-exceptions --disable-shared --disable-tls --enable-languages=c,d,c++ --enable-lto --with-float=soft --with-fpu=vfp --with-arch=armv5te --enable-target-optspace --enable-shared--prefix=/arm/ndk-git/ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86 --enable-initfini-array --disable-nls --prefix=/arm/ndk-git/ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86 --with-sysroot=/arm/ndk-git/ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/sysroot --with-binutils-version=2.19 --with-mpfr-version=2.4.1 --with-gmp-version=4.2.4 --with-gcc-version=4.4.3 --with-gdb-version=6.6 --with-arch=armv5te --enable-libstdc__-v3 --disable-werrors --enable-languages=c,d,c++ --enable-shared --enable-multilib --disable-threads --program-transform-name='s,^,arm-linux-androideabi-,'
Thread model: single
gcc version 4.4.3 20100121 (gdc hg, using dmd 1.067) (GCC) 
COLLECT_GCC_OPTIONS='-v' '-mthumb' '-shared-libgcc' '-pthread' '-march=armv5te' '-mfloat-abi=soft' '-mfpu=vfp'
 /home/dan/projects/arm-linux-androideabi-4.4.3/bin/../libexec/gcc/arm-linux-androideabi/4.4.3/cc1d gdc_test.d -mbionic -fpic -quiet -dumpbase gdc_test.d -mthumb -march=armv5te -mfloat-abi=soft -mfpu=vfp -auxbase gdc_test -version -imultilib thumb -iprefix /home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/ -isysroot /home/dan/projects/arm-linux-androideabi-4.4.3/bin/../sysroot -fmultilib-dir=thumb -o /tmp/cc2yu97y.s
GNU D (GCC) version 4.4.3 20100121 (gdc hg, using dmd 1.067) (arm-linux-androideabi)
    compiled by GNU C version 4.5.2, GMP version 4.2.4, MPFR version 2.4.1.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU D (GCC) version 4.4.3 20100121 (gdc hg, using dmd 1.067) (arm-linux-androideabi)
    compiled by GNU C version 4.5.2, GMP version 4.2.4, MPFR version 2.4.1.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
COLLECT_GCC_OPTIONS='-v' '-mthumb' '-shared-libgcc' '-pthread' '-march=armv5te' '-mfloat-abi=soft' '-mfpu=vfp'
 /home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/as -v -march=armv5te -mfloat-abi=soft -mfpu=vfp -meabi=5 -o /tmp/ccuFGEfj.o /tmp/cc2yu97y.s
GNU assembler version 2.19 (arm-linux-androideabi) using BFD version (GNU Binutils) 2.19
COMPILER_PATH=/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../libexec/gcc/arm-linux-androideabi/4.4.3/:/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../libexec/gcc/:/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/
LIBRARY_PATH=/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/thumb/:/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/lib/thumb/:/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/:/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/:/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/lib/:/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../sysroot/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-mthumb' '-shared-libgcc' '-pthread' '-march=armv5te' '-mfloat-abi=soft' '-mfpu=vfp'
 /home/dan/projects/arm-linux-androideabi-4.4.3/bin/../libexec/gcc/arm-linux-androideabi/4.4.3/collect2 --sysroot=/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../sysroot --eh-frame-hdr -dynamic-linker /system/bin/linker -X -m armelf_linux_eabi /home/dan/projects/arm-linux-androideabi-4.4.3/bin/../sysroot/usr/lib/crtbegin_dynamic.o -L/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/thumb -L/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/lib/thumb -L/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3 -L/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc -L/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/lib -L/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../sysroot/usr/lib /tmp/ccuFGEfj.o -lgphobos -lm -lrt -lgcc_s -lgcc -lpthread -lc -ldl -lgcc_s -lgcc /home/dan/projects/arm-linux-androideabi-4.4.3/bin/../sysroot/usr/lib/crtend_android.o
/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: cannot find -lrt
collect2: ld returned 1 exit status

Is there a way to override this behavior?

Cormick answered 19/7, 2011 at 20:55 Comment(0)
S
48

When you see a -lsomething, the library name is libsomething. Doing a man librt says the following:

NAME
     librt, libposix4 - POSIX.1b Realtime Extensions library

SYNOPSIS
     cc [ flag... ] file... -lrt [ library... ]

DESCRIPTION
     Functions in this library provide  most  of  the  interfaces
     specified  by  the  POSIX.1b  Realtime Extension.  See stan-
     dards(5). Specifically, this includes the interfaces defined
     under   the   Asynchronous  I/O,  Message  Passing,  Process
     Scheduling, Realtime Signals Extension,  Semaphores,  Shared
     Memory  Objects,  Synchronized  I/O, and Timers options. The
     interfaces defined under the Memory  Mapped  Files,  Process
     Memory  Locking,  and  Range Memory Locking options are pro-
     vided in libc(3LIB)

     See the man pages for the individual interfaces  in  section
     3RT for information on required headers.

     The name libposix4 is maintained for backward  compatibility
     and  should be avoided. librt is the preferred name for this
     library.
Sanderlin answered 19/7, 2011 at 21:0 Comment(5)
Right, but this is Bionic, not a glibc install. Bionic integrates libpthreads and librt into libc, I believe.Cormick
May I ask what version of Unix/Linux it is? I tried with RHEL and Ubuntu but got No manual entry for librtMarrowfat
I don't recall, I answered that many years ago :) However it's worth noting that you may not have the man pages installed since on Ubuntu that tends to be in a standalone package.Sanderlin
@Cormick In Solaris, librt is a "filter" on libc, whatever that means: "The shared object is implemented as a filter on libc.so.1. New application development need not specify –lrt." Maybe it's the same with Ubuntu. docs.oracle.com/cd/E36784_01/html/E36873/librt-3lib.htmlFootnote
upvoted. "pure complication". i found this very hard way.Coffer
H
8

-l is the flag used to link a library into your final executable. In this case, the library in question is librt.a (or librt.so or librt.dylib, depending on your system). librt is the realtime extensions library.

Heterochromatic answered 19/7, 2011 at 20:57 Comment(4)
So -L/path/to/libc should help ld find this?Cormick
No, libc is the C runtime library. rt is real-time extensions also needed for threads etc.Sally
Sorry, that is something you need to take up with the Bionic guys. On Linux, we're happy with librt.so and librt.a. But for starters, what happens if you change the Makefile to not use -lrt -- missing symbols or does it build?Sally
It's implicitly added by gdc in the link process. That said, I'll try the -v option and post the results when I get back to the workstation that has the toolchain I am working with. Thanks for the answers, regardless.Cormick
C
5

Answering my own question: it was hardcoded into gdc/d/d-spec.c

Cormick answered 20/7, 2011 at 2:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.