2

I am building an executable and a .so file using another .so

mylib.so: mylib.o
     ld -shared -o mylib.so packer.o mylib.o -ldl -L../lib -lcustombuild
server: server.o packer.o
     gcc packer.o server.o -o server -L../lib -lcustombuild

The file libcustombuild is in dir lib one level above current dir (i.e. ../lib) When I run my ./server it throws error: error while loading shared libraries: libcustombuild.so: cannot open shared object file: No such file or directory I am sure the .so file is in right directory.
ls ../lib output: libcustombuild.so

1
  • -L provides a folder for the linker to look in for libraries while linking. It cannot help with loading an so at runtime. the so needs to be in the path somewhere. Feb 2, 2016 at 3:09

3 Answers 3

2

-L only tells the linker where to find the link-time library. The .so file still needs to be known to the run-time library loader.

So you either need to use something like the LD_LIBRARY_PATH environment variable, or the rpath linker options, to tell the loader where to find your .so file, or put the .so file into one of the system library paths.

See rpath - Wikipedia for more information.

1
  • Thanks. I changed the line to: ld -shared -o mylib.so packer.o mylib.o -ldl -L../lib -lcustombuild -Wl,-rpath=../lib and now I get this error: unrecognized option '-Wl,-rpath=../lib'
    – Neo
    Feb 2, 2016 at 3:15
0

Okay, so here is how I fixed it:

mylib.so: mylib.o
     ld -shared -o mylib.so packer.o mylib.o -ldl -L../lib -lcustombuild
server: server.o packer.o
     gcc packer.o server.o -o server -L../lib -lcustombuild

and

export LD_LIBRARY_PATH=../lib

-Wl,-rpath wasn't a valid command for ld

0

Yes, -Wl,-rpath is not a valid command for ld. In fact it is an option for gcc/g++ as a linker option. -Wl means pass the parameter after the comma to the linker when calls it.

And, since rpath indicates to hard-code the path to the binary file, I think it is not a good idea to use relative path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.