Reputation: 45
Im trying to link my program to the shared library. Im using a makefile to compile. It looks like this: `
make: sms_out.cpp SMSDispatch.cpp SMSDispatch.h
g++ -c -fPIC SMSDispatch.cpp -o SMSDispatch.o
g++ -shared SMSDispatch.o -o libSMSDispatch.so
` g++ sms_out.cpp -L. -lSMSDispatch -o sms_out
It works fine if I run the program in the command window with:
LD_LIBRARY_PATH="." ./sms_out
But I want to run it with just ./sms_out
, can someone help me?
Tried to add export LD_LIBRARY_PATH=.
to the makefile, but that didnt work, just got the error " error while loading shared libraries: libSMSDispatch.so: cannot open shared object file: No such file or directory" when I try to run the program.
Upvotes: 3
Views: 3686
Reputation: 5110
Another option - provide -rpath options to linker to inform your binary where else search for dynamic objects.
g++ -Wl,-rpath=<path to .so> -o <your binary here> <cpp file name>.cpp
Upvotes: 3
Reputation: 121971
Add the directory where the .so
file exists to LD_LIBRARY_PATH
:
$ export LD_LIBRARY_PATH=/dir/containing/sharedobject
A utility you may find useful is ldd
, which prints the shared library dependencies. For example:
$ ldd /bin/ls linux-vdso.so.1 => (0x00007fff819ff000) librt.so.1 => /lib64/librt.so.1 (0x00007fc0d3f67000) libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fc0d3d4a000) libacl.so.1 => /lib64/libacl.so.1 (0x00007fc0d3b42000) libc.so.6 => /lib64/libc.so.6 (0x00007fc0d37e9000) libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fc0d35cd000) /lib64/ld-linux-x86-64.so.2 (0x00007fc0d4170000) libdl.so.2 => /lib64/libdl.so.2 (0x00007fc0d33c9000) libattr.so.1 => /lib64/libattr.so.1 (0x00007fc0d31c4000)
If shared objects are not locatable a string not found, or similar, is displayed instead of the path to the shared object being used.
Upvotes: 2