【发布时间】:2018-11-10 16:25:36
【问题描述】:
我正在使用 CentOS 机器。我有一个文件 (test.c) 正在编译到共享库中。
test.c:
#include <stdio.h>
#include <stdlib.h>
#include <quadmath.h>
int my_func(void){
__float128 r;
r = strtoflt128 ("1.2345678", NULL);
int * b = malloc(sizeof(int) * 5);
double a = 10*M_PI_2q;
printf("Hello world %f\n",a);
return 0;
}
编译它:
$ gcc -c -fPIC test.c -o test.o
$ gcc -shared -Wl,--verbose -Wl,-soname,poo.so -Wl,-rpath,/opt/gcc/5.5.0/lib/ -o poo.so test.o -lquadmath
$ echo $LD_LIBRARY_PATH ### This shouldn't matter?
/opt/gcc/5.5.0/lib
从ld 的详细输出来看,似乎找到了/opt/gcc/5.5.0/lib64/libquadmath.so.0(这是我想要的)。即
.
.
.
attempt to open /opt/gcc/5.5.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.5.0/libquadmath.so failed
attempt to open /opt/gcc/5.5.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.5.0/libquadmath.a failed
attempt to open /opt/gcc/5.5.0/bin/../lib/gcc/libquadmath.so failed
attempt to open /opt/gcc/5.5.0/bin/../lib/gcc/libquadmath.a failed
attempt to open /opt/gcc/5.5.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.5.0/../../../../lib64/libquadmath.so succeeded
-lquadmath (/opt/gcc/5.5.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.5.0/../../../../lib64/libquadmath.so)
.
.
.
但是,当我查看 ldd poo.so 时,它没有有正确的库。即
$ ldd poo.so
linux-vdso.so.1 => (0x00007fffc0e8e000)
libquadmath.so.0 => /act/gcc-4.7.2/lib64/libquadmath.so.0 (0x00002aea8fd39000)
libc.so.6 => /lib64/libc.so.6 (0x00002aea8ff6f000)
libm.so.6 => /lib64/libm.so.6 (0x00002aea90303000)
/lib64/ld-linux-x86-64.so.2 (0x00002aea8f8f2000)
我正在使用gcc 5.5.0 版本,并希望poo.so 链接到/opt/gcc/5.5.0/lib/libquadmath.so.0 而不是4.7.2 版本。
我认为这与/etc/ld.so.conf 中存在指定旧库位置的文件有关。即
$ cat /etc/ld.so.conf.d/gcc-4.7.2.conf
/act/gcc-4.7.2/lib64
/act/gcc-4.7.2/lib
ld 的手册页(正如我所询问的 here)没什么用处。
问题:我在链接和构建共享库时指定了-rpath,但我不明白为什么它会找到较旧的 gcc-4.7.2 版本的 quadmath 库。为什么链接器似乎优先考虑/etc/ld.so.conf 中的路径而忽略-rpath 选项?如何在编译时指定库位置以及如何解决此问题?
【问题讨论】: