构建共享对象库:ldd不显示指定的名称

时间:2022-09-18 12:26:25

I'm trying to build a shared object library on Debian

我正在尝试在Debian上构建一个共享对象库

cat /etc/issue
Debian GNU/Linux 9 \n \l

I build the library and object as normal (wrap.c serves as a wrapper to create all object files)

我正常地构建库和对象(wrap)。c作为一个包装器来创建所有的对象文件)

gcc -c -fPIC -W -Wall -O2 -funroll-loops wrap.c
gcc -shared -Wl,-soname,libtest.so -o libtest.so *.o
mv libtest.so /usr/local/lib/ && mv test-header.h /usr/local/include/

I then create a test.c to pull in the library and compile successfully as follows:

然后我创建一个测试。c .拉入库,成功编译如下:

gcc test.c -ltest

gcc测试。c -ltest

However, running the program ./a.out returns the following error:

然而,运行程序。输出返回以下错误:

./a.out: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory

,/。out:加载共享库时出错:libtest。无法打开共享对象文件:没有这样的文件或目录

Inspecting the .so, I see:

所以,我明白了:

$ ldd /path/to/libtest.so
    linux-vdso.so.1 (0x00007ffdb71c5000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f1c22fba000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f1c23560000)

I don't even see libtest.so => none, which at least would tell me it can't find the library.

我甚至看不到libtest。所以=>,至少会告诉我它找不到库。

I'm not really sure what's going on here.

我不太清楚这里发生了什么。

I am to successfully create a .dylib on macOS with the same process (with gcc -dynamiclib -o libtest.dylib *.o), and I can successfully call the library in an executable. I'm not sure what's different on Debian.

我将使用相同的进程(使用gcc -dynamiclib -o libtest)在macOS上成功创建一个.dylib。dylib *.o),我可以成功地调用可执行文件中的库。我不知道Debian有什么不同。

1 个解决方案

#1


2  

The shared library libtest.so that you have placed in /usr/local/lib will be located by linker in the command

共享库libtest。因此,将您放置在/usr/local/lib中,将由linker在命令中定位。

gcc test.c -ltest

because /usr/local/lib is one the linker's default search paths.

因为/usr/local/lib是链接器的默认搜索路径之一。

However, it will not be located there by the runtime loader when you attempt to run ./a.out because the runtime loader does not search directories directly other than those listed in the value of the variable LD_LIBRARY_PATH, if any, in the current environment. By default it searchs the libraries registered in the ldconfig cache, and that cache is updated to register newly appeared libraries only by running ldconfig (as root).

但是,当您尝试运行时,它不会被运行时加载器定位。因为运行时加载程序不会直接搜索当前环境中变量LD_LIBRARY_PATH中的目录(如果有的话)。默认情况下,它搜索在ldconfig缓存中注册的库,并且该缓存仅通过运行ldconfig(作为根)来更新以注册新出现的库。

So to run your program you have two options:-

所以要运行你的程序你有两个选择:-

For success in your current shell, run:

在你当前的shell中,运行:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib; ./a.out

For lasting success, run:

持久的成功,运行:

sudo ldconfig

Then your program will run in any shell.

然后你的程序将在任何shell中运行。

BTW, ldd /path/to/libtest.so tells you, of course, the shared library dependencies of libtest.so. That's not going to tell you why running ./a.out fails to find /path/to/libtest.so itself. To see the shared library dependencies of a.out, run ldd a.out

顺便说一句,ldd /路径/ / libtest。当然,告诉您libtest的共享库依赖项。这并不能告诉你为什么要跑步。out未能找到/path/to/libtest。所以本身。查看a的共享库依赖项。,运行ldd a.o ut

#1


2  

The shared library libtest.so that you have placed in /usr/local/lib will be located by linker in the command

共享库libtest。因此,将您放置在/usr/local/lib中,将由linker在命令中定位。

gcc test.c -ltest

because /usr/local/lib is one the linker's default search paths.

因为/usr/local/lib是链接器的默认搜索路径之一。

However, it will not be located there by the runtime loader when you attempt to run ./a.out because the runtime loader does not search directories directly other than those listed in the value of the variable LD_LIBRARY_PATH, if any, in the current environment. By default it searchs the libraries registered in the ldconfig cache, and that cache is updated to register newly appeared libraries only by running ldconfig (as root).

但是,当您尝试运行时,它不会被运行时加载器定位。因为运行时加载程序不会直接搜索当前环境中变量LD_LIBRARY_PATH中的目录(如果有的话)。默认情况下,它搜索在ldconfig缓存中注册的库,并且该缓存仅通过运行ldconfig(作为根)来更新以注册新出现的库。

So to run your program you have two options:-

所以要运行你的程序你有两个选择:-

For success in your current shell, run:

在你当前的shell中,运行:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib; ./a.out

For lasting success, run:

持久的成功,运行:

sudo ldconfig

Then your program will run in any shell.

然后你的程序将在任何shell中运行。

BTW, ldd /path/to/libtest.so tells you, of course, the shared library dependencies of libtest.so. That's not going to tell you why running ./a.out fails to find /path/to/libtest.so itself. To see the shared library dependencies of a.out, run ldd a.out

顺便说一句,ldd /路径/ / libtest。当然,告诉您libtest的共享库依赖项。这并不能告诉你为什么要跑步。out未能找到/path/to/libtest。所以本身。查看a的共享库依赖项。,运行ldd a.o ut