Fre*_*ner 50 c++ shared-libraries dynamic-linking
我有一个与另一个(第三方)共享库链接的共享库.然后在我的应用程序中使用dlopen加载我的共享库.所有这一切都很好(假设文件在正确的路径等).
现在,问题是当我链接我的库时,我甚至不需要指定链接第三方共享库.GCC接受它而不报告有关未定义引用的错误.那么,问题; 我如何强制GCC通知我未定义的引用?
如果我将库更改为(临时)可执行文件,则会获得未定义的引用(当不向链接器提供库时).(如果我指定它,工作正常.)
即,完成以下操作:
g++ -fPIC -shared -o libb.so b.o
g++ -fPIC -shared -o liba.so a.o
g++ -o a.exe a.cpp
Run Code Online (Sandbox Code Playgroud)
第二行没有给出错误,第三行抱怨未定义的引用.
示例代码:
啊:
class a
{
public:
void foobar();
};
Run Code Online (Sandbox Code Playgroud)
a.cpp:
#include "a.h"
#include "b.h"
void a::foobar()
{
b myB;
myB.foobar();
}
int main()
{
a myA; myA.foobar();
}
Run Code Online (Sandbox Code Playgroud)
BH:
class b
{
public:
void foobar();
};
Run Code Online (Sandbox Code Playgroud)
b.cpp:
#include "b.h"
void b::foobar()
{
}
Run Code Online (Sandbox Code Playgroud)
Dmi*_*kov 52
-Wl, -构建共享库时可以使用no-undefined链接器选项,未定义的符号将显示为链接器错误.
g ++ -shared -Wl,-soname,libmylib.so.5 -Wl, - no-undefined -o libmylib.so.1.1 mylib.o -lthirdpartylib
P S*_*ved 19
经过更多的研究,我意识到这些东西的运作方式.有两个链接器选项可以操作共享库的未定义符号:
第一个是--no-undefined
.它报告在链接阶段未立即解决的未解析符号.除非在链接的共享库中找到符号,无论是手动(使用-l
switch)还是自动(使用libgcc_s
C++运行时; libc
C运行时; ld-linux-**.so
动态链接器工具),都会--no-undefined
将其报告为错误.这是提问者所需要的关键.
还有另一个关键,--no-allow-shlib-undefined
(其描述也表明--no-undefined
).它检查是否满足共享库中与您的共享库链接的定义.在本主题中显示的情况下,此键很少使用,但它可能很有用.但是,它有自己的障碍.
该联机帮助页提供了一些关于它为什么不是默认值的理由:
--allow-shlib-undefined
--no-allow-shlib-undefined
Allows (the default) or disallows undefined symbols in shared
libraries (It is meant, in shared libraries _linked_against_, not the
one we're creating!--Pavel Shved). This switch is similar to --no-un-
defined except that it determines the behaviour when the undefined
symbols are in a shared library rather than a regular object file. It
does not affect how undefined symbols in regular object files are
handled.
The reason that --allow-shlib-undefined is the default is that the
shared library being specified at link time may not be the same as
the one that is available at load time, so the symbols might actually
be resolvable at load time. Plus there are some systems, (eg BeOS)
where undefined symbols in shared libraries is normal. (The kernel
patches them at load time to select which function is most appropri-
ate for the current architecture. This is used for example to dynam-
ically select an appropriate memset function). Apparently it is also
normal for HPPA shared libraries to have undefined symbols.
Run Code Online (Sandbox Code Playgroud)
问题是上述内容也是如此,例如,对于Linux系统,其中共享库的一些内部例程是在ld-linux.so
动态加载器(它是可执行文件和共享库)中实现的.除非你以某种方式链接它,你会得到这样的东西:
/lib64/libc.so.6: undefined reference to `_dl_argv@GLIBC_PRIVATE'
/lib64/libc.so.6: undefined reference to `_rtld_global_ro@GLIBC_PRIVATE'
/usr/lib64/gcc/x86_64-suse-linux/4.3/libstdc++.so: undefined reference to `__tls_get_addr@GLIBC_2.3'
/lib64/libc.so.6: undefined reference to `_rtld_global@GLIBC_PRIVATE'
/lib64/libc.so.6: undefined reference to `__libc_enable_secure@GLIBC_PRIVATE'
Run Code Online (Sandbox Code Playgroud)
这些是来自加载器的未定义引用ld-linux.so
.它是特定于平台的(例如,在我的系统上正确的加载器是/lib64/ld-linux-x86-64.so
).您可以将加载程序与库链接,并检查上面显示的棘手的引用:
g++ -fPIC -shared -o liba.so a.o -Wl,--no-allow-shlib-undefined /lib64/ld-linux-x86-64.so.2
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
35619 次 |
最近记录: |