简体   繁体   English

链接到AIX上的共享库

[英]Linking against a shared library on AIX

I'm trying to link against a shared library (apr) on AIX 5.3 using gcc/libtool. 我正在尝试使用gcc / libtool链接到AIX 5.3上的共享库(apr)。

The output from the compiler is as follows (with some irrelevant flags removed for the sake of simplicity): 编译器的输出如下(为简单起见,删除了一些不相关的标志):

libtool: link: gcc  -o test test.o  -L/opt/freeware/lib -lapr-1 -lpthread -Wl,-blibpath:/opt/freeware lib:/usr/lib:/lib 

Then I checked what shared libs the resulting binary uses: 然后,我检查了生成的二进制文件使用了哪些共享库:

$ ldd test 
test needs:
     /usr/lib/libc.a(shr.o)
     /usr/lib/libpthread.a(shr_xpg5.o)
     /unix
     /usr/lib/libcrypt.a(shr.o)
     /usr/lib/libpthreads.a(shr_comm.o)

Notice that 'libapr-1' is missing here, though the symbols are there in the binary (verified with nm), which suggests that it is linked in statically. 请注意,尽管二进制文件中存在符号(已通过nm验证),但这里缺少'libapr-1',这表明它是静态链接的。

This wouldn't be such a big problem for simple programs. 对于简单的程序来说,这并不是什么大问题。 Unfortunately my code in question uses dynamically loadable modules. 不幸的是,我所讨论的代码使用可动态加载的模块。 The main program calls apr_initialize which sets a static variable 'apr_pools_initialized' inside the library. 主程序调用apr_initialize,它在库中设置一个静态变量'apr_pools_initialized'。 The loadable modules then try to use apr_pool_create which first check whether the initialization has been performed. 然后,可加载模块尝试使用apr_pool_create,后者首先检查是否已执行初始化。 Since they have their own statically linked apr, the static variable 'apr_pools_initialized' is not at the same memory location what the main program initialized. 由于它们具有自己的静态链接的apr,因此静态变量'apr_pools_initialized'与主程序初始化的内存位置不同。 This makes the statically linked binary non-functional. 这使静态链接的二进制文件不起作用。

The apr library is installed using a precompiled binary rpm (apr and apr-devel). 使用预编译的二进制rpm(apr和apr-devel)安装apr库。 The relevant library files are there: 相关的库文件位于:

# rpm -ql apr|grep \\.so$
/opt/freeware/lib/libapr-1.so
/opt/freeware/lib64/libapr-1.so
/usr/lib/libapr-1.so

# rpm -ql apr-devel|grep \\.a$
/opt/freeware/lib/libapr-1.a
/opt/freeware/lib64/libapr-1.a
/usr/lib/libapr-1.a
/usr/lib64/libapr-1.a
/usr/lib64/libapr-1.so

I tried to remove the '.a' files hoping that the linker would have no choice but to use the '.so' and link it dynamically, unfortunately AIX is different and this does not work. 我试图删除'.a'文件,希望链接器别无选择,只能使用'.so'并动态链接它,不幸的是AIX是不同的,因此不起作用。 Regarding this topic I have found this answer and another libtool question which give some insight. 关于这个主题,我找到了这个答案和另一个提供一些见解的libtool问题

The question is: How can I link this to my binary dynamically? 问题是: 如何将其动态链接到二进制文件?

Actually the links referenced contained the solution to this problem, which is: 实际上,引用的链接包含此问题的解决方案,即:

-Wl,-brtl

Adding these LDFLAGS solved the linking problem. 添加这些LDFLAGS解决了链接问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM