在linux中使用共享库执行二进制文件

Executing a binary with a shared library in linux

本文关键字:执行 二进制文件 共享 linux      更新时间:2023-10-16

我正在制作一个简单的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 => not found"它自动查找的其他库没有问题。

有谁知道这是为什么吗?

共享库的位置不在链接器的搜索路径中。您可以通过将库所在的目录添加到LD_LIBRARY_PATH环境变量中来确认这一点,然后再次运行ldd。详细信息请参见ld.so(8)手册页。