Neo
Neo

Reputation: 391

Linker cannot find .so file

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

Upvotes: 2

Views: 8110

Answers (3)

Holsety
Holsety

Reputation: 103

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.

Upvotes: 0

Neo
Neo

Reputation: 391

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

Upvotes: 0

TBBle
TBBle

Reputation: 1466

-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.

Upvotes: 2

Related Questions