链接AIX上的共享库

时间:2021-07-04 06:21:48

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.

请注意,这里缺少'libapr-1',尽管二进制符号中存在符号(用nm验证),这表明它是静态链接的。

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. The loadable modules then try to use apr_pool_create which first check whether the initialization has been performed. 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. This makes the statically linked binary non-functional.

对于简单的程序来说,这不是一个大问题。不幸的是,我的代码使用动态可加载模块。主程序调用apr_initialize,它在库中设置一个静态变量'apr_pools_initialized'。然后可加载模块尝试使用apr_pool_create,首先检查是否已执行初始化。由于它们具有自己的静态链接apr,因此静态变量“apr_pools_initialized”与主程序初始化的内存位置不同。这使得静态链接的二进制不起作用。

The apr library is installed using a precompiled binary rpm (apr and apr-devel). The relevant library files are there:

使用预编译的二进制rpm(apr和apr-devel)安装apr库。相关的库文件在那里:

# 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. Regarding this topic I have found this answer and another libtool question which give some insight.

我试图删除'.a'文件,希望链接器别无选择,只能使用'.so'并动态链接它,遗憾的是AIX不同,这不起作用。关于这个主题,我找到了这个答案和另一个libtool问题,提供了一些见解。

The question is: How can I link this to my binary dynamically?

问题是:如何动态地将其链接到我的二进制文件?

1 个解决方案

#1


0  

Actually the links referenced contained the solution to this problem, which is:

实际上引用的链接包含这个问题的解决方案,即:

-Wl,-brtl

Adding these LDFLAGS solved the linking problem.

添加这些LDFLAGS解决了链接问题。

#1


0  

Actually the links referenced contained the solution to this problem, which is:

实际上引用的链接包含这个问题的解决方案,即:

-Wl,-brtl

Adding these LDFLAGS solved the linking problem.

添加这些LDFLAGS解决了链接问题。