简体   繁体   中英

g++ shared library only links in current directory

I'm trying to compile a C++ program (on Linux and OS X) with a library that I built in another directory:

$ g++ -I./include -L./lib src/main.cpp -lale -o proj

"ale" is just the library I'm using (libale.so in ./lib/libale.so )

It compiles everything, but when I run it I get the following error:

./proj: error while loading shared libraries: libale.so: cannot open shared object file: No such file or directory

However, it does work if I put libale.so into the same directory as the executable (or add a symlink). I was looking up some solutions, and one suggestion was to set LD_LIBRARY_PATH . However, I would rather link the path directly during compilation. Is there a way to do that?

The answer I found was to add the following flag:

-Wl,-rpath=./lib

However, this does not work on OS X. I ended up just using a symlink for the OS X version, but it isn't the optimal solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM