Sir Rogers Sir Rogers - 3 months ago 14
Linux Question

Linux - Why does my executable have libpthread as a shared library?

So basically I´d like to know why my executable has libpthread as a shared library whilst my code is not using any threading functions whatsoever. It isn´t included from the makefile either. Is it because GCC is compiled by default with "--enable-threads=posix" ?

If so is there a way to remove it from my executable? If not what is the problem?

ldd

linux-vdso.so.1 => (0x0000656150c5a000)
librt.so.1 => /lib64/librt.so.1 (0x0000656150a4c000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x0000656150746000)
libm.so.6 => /lib64/libm.so.6 (0x00006561504c1000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00006561502ab000)
libc.so.6 => /lib64/libc.so.6 (0x000065614ff17000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x000065614fcf9000)
/lib64/ld-linux-x86-64.so.2 (0x0000656150c5b000)


Makefile

CC = gcc
OFLAGS = -O3

C++ = g++ -g
LFLAGS = -lrt

# API Exclusions
DFLAGS = -DNO_ZLIB -DNO_LOCALIZATION -DNO_INTERFACE

CFLAGS = $(OFLAGS) $(DFLAGS) -I. -I../rgapi/include/core/ -I../rgapi/include/public/

OBJS = ( ... list of .o files ... )
PROGS = ./rgs

all: $(OBJS) $(PROGS)

./rgs: $(OBJS)
$(C++) -o ./rgs $(OBJS) $(LFLAGS)

clean:
rm -f $(OBJS) $(PROGS)

$(OBJS): %.o: %.cpp
$(C++) -o $@ $(CFLAGS) -c $<

./rgs: $(OBJS)

Answer

You are linking with librt.so:

LFLAGS = -lrt

This is an indirect dependency.

$ ldd /usr/lib64/librt.so
    linux-vdso.so.1 (0x00007ffcc33d2000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f2480a0b000)
    libc.so.6 => /lib64/libc.so.6 (0x00007f2480649000)
    /lib64/ld-linux-x86-64.so.2 (0x0000562f4cffb000)

librt.so is linked with -lpthread. If you link with any shared library, you inherit all the baggage that the shared library gets linked with.

You cannot "remove it from your executable". The only way to do so is not link with -lrt.

Comments