1

我已经在 RHEL6 机器上安装了 devtoolset-3,我对 gcc/g++ 如何确定哪个库与二进制文件链接感到困惑。

RHEL6 上的标准设置:

$ g++ -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) 

在我编译一个简单的、几乎为空的 C++ 二进制文件后,我得到:

$ g++ main.cpp -o main_old && ldd main_old
        linux-vdso.so.1 =>  (0x00007fffe6dfe000)
        libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00000039b4800000)
        libm.so.6 => /lib64/libm.so.6 (0x00000039aa800000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00000039b4400000)
        libc.so.6 => /lib64/libc.so.6 (0x00000039aa400000)
        /lib64/ld-linux-x86-64.so.2 (0x00000039aa000000)
$ echo $PATH
/usr/local/bin:/usr/bin:/usr/sbin/:/bin/:/sbin
$ echo $LD_LIBRARY_PATH

然后我会安装devtoolset-3(从这里https://www.softwarecollections.org/en/scls/rhscl/devtoolset-3/

接着

$ scl enable devtoolset-3 bash
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-3/root/usr/libexec/gcc/x86_64-redhat-linux/4.9.1/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/opt/rh/devtoolset-3/root/usr --mandir=/opt/rh/devtoolset-3/root/usr/share/man --infodir=/opt/rh/devtoolset-3/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --enable-languages=c,c++,fortran,lto --enable-plugin --with-linker-hash-style=gnu --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.9.1-20140922/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.9.1-20140922/obj-x86_64-redhat-linux/cloog-install --with-mpc=/builddir/build/BUILD/gcc-4.9.1-20140922/obj-x86_64-redhat-linux/mpc-install --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.9.1 20140922 (Red Hat 4.9.1-10) (GCC)
$ g++ main.cpp -o main_new && ldd main_new
       linux-vdso.so.1 =>  (0x00007fffe18ae000)
        libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00000039b4800000)
        libm.so.6 => /lib64/libm.so.6 (0x00000039aa800000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00000039b4400000)
        libc.so.6 => /lib64/libc.so.6 (0x00000039aa400000)
        /lib64/ld-linux-x86-64.so.2 (0x00000039aa000000)
$ echo $PATH
/usr/lib64/qt-3.3/bin:/opt/rh/devtoolset-3/root/usr/bin/:/opt/rh/devtoolset-3/root/usr/bin:/usr/local/bin:/usr/bin:/usr/sbin/:/bin/:/sbin
$ echo $LD_LIBRARY_PATH
/opt/rh/devtoolset-3/root/usr/lib64:/opt/rh/devtoolset-3/root/usr/lib

我也一样...

4

1 回答 1

4

GCC 本身并没有选择。虽然 GCC(实际上ld)会在链接时(通过自己的路径搜索顺序)查找和检查动态库,但它只是DT_NEEDED为库的SONAME. 看一下DYNAMIC带有 的部分readelf

在运行时,ld.so选择要链接的库。选择的特定库ld.so由其配置的路径搜索顺序决定。请参阅手册页ld.so

于 2015-07-01T11:14:26.543 回答