gcc构建链接,但共享库不会与ldd一起显示 [英] gcc build links but shared library does not appear with ldd
问题描述
我有一个我必须建立的程序。该程序依赖于 libA
和 libA
取决于 libB
。这两个库位于同一个文件夹中,但 ldd libA.so
不包含 libB.so
,所以我必须添加它链接它。
这是我的 gcc
命令:
< blockquote>
gcc -L / path / to / libraries / lib -lA -lB -I / path / to / libraries / include main.cpp
程序会建立并链接,但不会启动。它给了我下面的错误:
./ a.out:符号查找错误:/path/to/libraries/lib/libA.so :undefined symbol:symbol_used_in_libA_but_defined_in_libB
使用 ldd
我可以看到<$ c $ b>
linux-vdso.so。 1 => (0x00007fffaecd9000)
libA.so => /path/to/libraries/lib/libA.so(0x00007effc02a4000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6(0x00007effbfebb000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1(0x00007effbfca5000)
/lib64/ld-linux-x86-64.so.2(0x00007effc05cb000)
我有这些条件:
/ path / to / libraries
位于 LD_LIBRARY_PATH
ldconfig
可以,并且
ldconfig -p
找到 libA.so
和 libB .so
-lB
改为 -lBB
它给了我一个链接器错误,所以我认为 gcc
找到正确 libB.so
即使它没有将其链接到可执行文件中。
我做错了什么?为了将可执行文件链接到两个库,我可以做些什么?解析方案
大多数Linux发行版(我假设你使用的是基于Linux的版本在 ldd
的输出上)似乎将 gcc
配置为通过 - 根据需要
默认为 DT_NEEDED
标签)在你的情况下, main.cpp
不会使用 libB的任何函数。 $ b
所以链接器不会添加 libB
作为最终可执行文件的依赖关系。您可以通过将 - 不需要的
标志传递给链接器来解决它。例如,
gcc -Wl, - 不需要...
当然,正确的解决方法是重新链接 libA
并确保它列出 libB
作为依赖关系。
I've a program that I must build. The program depends on libA
, and libA
depends on libB
. Both libs are in the same folder but ldd libA.so
does not include libB.so
so I must add it while linking it.
This is my gcc
command:
gcc -L/path/to/libraries/lib -lA -lB -I/path/to/libraries/include main.cpp
The program builds and links, but it does not start. It gives me following error:
./a.out: symbol lookup error: /path/to/libraries/lib/libA.so: undefined symbol: symbol_used_in_libA_but_defined_in_libB
With ldd
I can see that libB.so
is not included in my binary:
linux-vdso.so.1 => (0x00007fffaecd9000)
libA.so => /path/to/libraries/lib/libA.so (0x00007effc02a4000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007effbfebb000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007effbfca5000)
/lib64/ld-linux-x86-64.so.2 (0x00007effc05cb000)
I have these conditions:
/path/to/libraries
is insideLD_LIBRARY_PATH
- running
ldconfig
is ok andldconfig -p
find bothlibA.so
andlibB.so
- If in gcc command I change
-lB
with-lBB
it gives me a linker error, so I think thatgcc
find correctlylibB.so
even if it does not link it inside the executable.
What I'm doing wrong? What I can do in order to link the executable to both libraries?
Most Linux distributions (I assume you are using Linux based on the output of ldd
) seem to configure gcc
as to pass --as-needed
to ld
by default (e.g., see here for Debian). This means the final library/executable will only depend on a library (i.e., have a DT_NEEDED
tag for that library) if some symbol of that library is actually used by the library/executable.
In your case, main.cpp
does not use any functions of libB
so the linker does not add libB
as a dependency of the final executable. You can work around it by passing the --no-as-needed
flag to the linker. E.g.,
gcc -Wl,--no-as-needed ...
Of course, the proper fix is to relink libA
and make sure it lists libB
as a dependency.
这篇关于gcc构建链接,但共享库不会与ldd一起显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!