Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake - Is it possible to link executable to shared library with relative path at runtime?

Lets say I have this:

../project_dir
    main.cpp
    mylib.cpp
    mylib.h

Building steps will be:

g++ -c mylib.cpp -o mylib.o
g++ -shared -o libmylib.so mylib.o
g++ -L$(pwd) -Wl,-rpath='$ORIGIN' -o exec main.cpp -lmylib

exec will be my binary executable output. When I testing with:

ldd exec

the output line is:

libmylib.so => /full/path/to/build/directory/libmylib.so (0x00007f75fdd1f000)

That output line is my question, is it possible to get:

libmylib.so => ./libmylib.so

so whenever I move the executable file, I can move the shared library along with it. If it is possible, how to do this with cmake?

like image 353
Mas Bagol Avatar asked Mar 16 '23 23:03

Mas Bagol


1 Answers

Juste add in your CMakefiles.txt

set(CMAKE_INSTALL_RPATH "$ORIGIN")
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) # only if you want copy from the build tree
like image 160
explo91 Avatar answered Mar 19 '23 23:03

explo91