megavore
megavore

Reputation: 127

Linked library not found at run-time (c++)

I have the following makefile

FLAGS = -g -c -Wall -Wextra -O2 -pedantic -Werror
CC = g++
SOURCE = $(wildcard src/*.cpp)
INTERM = $(patsubst %.cpp,%.o,$(SOURCE))
OBJECTS = $(patsubst src/%,nbobj/%,$(INTERM))
RES = app
INCLUDE = -I../../libs
OPENCV = -L/usr/local/lib `pkg-config --cflags --libs opencv`
LINK =  -L./../../libs -lgstd  $(OPENCV)


$(RES): $(OBJECTS)
    $(CC) $(OBJECTS) -o $(RES) $(LINK)

nbobj/%.o: src/%.cpp 
    $(CC) $(FLAGS) $(INCLUDE) -o $@ -c $<

clean:
    rm -rf $(RES) $(OBJECTS) 

It compiles fine, but when I run the program I get

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

This raises a couple of questions for me:

Furthermore, I am running into a similar problem where I have a library that contains a call to "clock_gettime". It compiles fine but at runtime, when I use a function from that library, I get "undefined reference to `clock_gettime'". How is it that when I compiled the library, the linker apparently found clock_gettime, but then when I used the library it couldn't be found, even though no files had been moved?

Thank you so much for your help :)

Upvotes: 0

Views: 1763

Answers (1)

user1810087
user1810087

Reputation: 5334

While starting your program, the os can't find the lib to load, especially it doesn't even know where to search, since ./../../libs is not a path where it will search. You can do 2 things. first, move the lib to a folder it will search (depending on your system)

/lib/ or /lib64/ or /usr/lib or ...

or second show your os where to search

export LD_LIBRARY_PATH=/absolut/path/to/libs

The parameter -L specifies additional paths where to search at link-time to the linker; NOT while runtime to the os. -l specifies additional libraries for the linker.

Upvotes: 3

Related Questions