g++/gcc 如何确定 link 有多个版本可供选择的库?
How does g++/gcc determine which library to link having more than 1 version to choose from?
我已经在 RHEL6 机器上安装了 devtoolset-3,但我对 gcc/g++ 如何用二进制文件 link 确定哪个库感到困惑。
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
我也一样...
GCC 本身不选择。虽然 GCC(实际上 ld
)会在 linking 时(通过它自己的路径搜索顺序)查找和检查动态库,但它只是为库的 SONAME
添加一个 DT_NEEDED
条目.看看 DYNAMIC
部分 readelf
.
在运行时,ld.so
选择要link 反对的库。 ld.so
选择的特定库由其配置的路径搜索顺序决定。请参阅 ld.so
.
的手册页
我已经在 RHEL6 机器上安装了 devtoolset-3,但我对 gcc/g++ 如何用二进制文件 link 确定哪个库感到困惑。
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
我也一样...
GCC 本身不选择。虽然 GCC(实际上 ld
)会在 linking 时(通过它自己的路径搜索顺序)查找和检查动态库,但它只是为库的 SONAME
添加一个 DT_NEEDED
条目.看看 DYNAMIC
部分 readelf
.
在运行时,ld.so
选择要link 反对的库。 ld.so
选择的特定库由其配置的路径搜索顺序决定。请参阅 ld.so
.