user690936 user690936 - 4 months ago 25
Linux Question

Makefile - cannot find shared library

I have a Makefile for a c++ Linux project:

MODE ?= dbg
DIR = ../../../../../somdir/$(MODE)

SRC_FILES = a.cpp b.cpp
H_FILES = a.h

LDFLAGS += -L$(DIR)/lib/linux '-Wl,-R$$ORIGIN'
CPPFLAGS = -I$(DIR)/include
LIBRARIES = -lsomeso

ifeq (rel, $(MODE))
CFLAGS = -Wall -g -DNDEBUG
else
CFLAGS = -Wall -ansi -pedantic -Wconversion -g -DDEBUG -D_DEBUG
endif

sample: $(SRC_FILES) $(H_FILES) Makefile
g++ $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(LIBRARIES) $(SRC_FILES) -o sample


when i run 'make' it builds the project, with no errors.
but when i run the project it complains that:

error while loading shared libraries: libsomeso.so: cannot open shared object file: No such file or directory


The path that i give in DIR goes to the folder where the shared object is held(relatively to where the makefile is placed), and if it was the wrong path why didn't it complain during the make process.

does someone know what am i missing?

Thanks
Matt

Answer
LDFLAGS += -L$(DIR)/lib/linux '-Wl,-R$$ORIGIN'

The above should be:

LDFLAGS += -L$(DIR)/lib/linux -Wl,-R$(DIR)/lib/linux '-Wl,-R$$ORIGIN'

That is, for each non-standard dynamic library location -L a corresponding -Wl,-R should be specified. $ORIGIN is needed to locate dynamic libraries relative to the executable, not sure if you need it here.

People often advise using LD_LIBRARY_PATH. This is a bad advice, in my opinion, because it makes deployment more complicated.

Comments