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

时间:2021-07-08 07:03:33

I am making a simple hello world program to learn about linking shared libraries in linux. I have managed to compile the main program into an executable with the shared library using the following:

我正在制作一个简单的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

When I do an ldd on hello_dyn I get an output stating that the library can't be found:

当我在hello_dyn上执行ldd时,我得到一个输出,指出无法找到库:

"libfuncs.so.1.0 => not found" The other libraries it looks for automatically are fine.

“libfuncs.so.1.0 => not found”它自动查找的其他库很好。

Anyone know why this might be?

谁知道为什么会这样?

1 个解决方案

#1


1  

Your shared library's location is not in the linker's search path. You can confirm this by adding the directory in which your library is located to the LD_LIBRARY_PATH environment variable and then run ldd again. See the ld.so(8) man page for details.

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

#1


1  

Your shared library's location is not in the linker's search path. You can confirm this by adding the directory in which your library is located to the LD_LIBRARY_PATH environment variable and then run ldd again. See the ld.so(8) man page for details.

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