我正在制作一个简单的 hello world 程序来学习如何在 Linux 中链接共享库。我已经设法使用以下方法将主程序编译成带有共享库的可执行文件:
g++ -fPIC -c lab2_hello_main.cpp <--create position independent objects
g++ -fPIC -c lab2_hello_sub.cpp
g++ -fPIC -shared -Wl,-soname=libfuncs.so.1.0 *.o -o libfuncs.so.1.0 -lc <--make the shared library
ln -s libfuncs.so.1.0 libfuncs.so <-- soft links for compiling and running
ln -s libfuncs.so.1.0 libfuncs.so.1
g++ -o hello_dyn lab2_hello_main.cpp -L/mypath -lfuncs <-- Linking the library to main
当我在 hello_dyn 上执行 ldd 时,我得到一个输出,指出找不到该库:
“libfuncs.so.1.0 => 未找到”
它自动查找的其他库都没有问题。
有人知道为什么会这样吗?
最佳答案
您的共享库的位置不在链接器的搜索路径中。您可以通过将您的库所在的目录添加到 LD_LIBRARY_PATH
环境变量来确认这一点,然后再次运行 ldd
。查看ld.so(8)手册页了解详细信息。
关于c++ - 在 Linux 中使用共享库执行二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22511887/