Shared library, makefile. Library path

I am trying to link my program to a shared library. I am 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 a command window with:

LD_LIBRARY_PATH="." ./sms_out

      

But I only want to run it with help ./sms_out

, can anyone help me? Tried adding export LD_LIBRARY_PATH=.

to the makefile but it didn't work, it just got the error "error loading shared libraries: libSMSDispatch.so: unable to open shared objects file: no such file or directory" when I try to run the program.

+3


source to share


2 answers


Add the directory where the .so

file exists LD_LIBRARY_PATH

:

$ export LD_LIBRARY_PATH=/dir/containing/sharedobject

      

A useful utility might be ldd

one that prints the dependencies of the shared library. 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 the shared object cannot be found, a string not found or similar is displayed instead of the path to the shared object in use.

+2


source


Another option is to provide the -rpath options to the linker to tell your binary where else to look for dynamic objects.



g++ -Wl,-rpath=<path to .so> -o <your binary here> <cpp file name>.cpp

      

+3


source







All Articles