首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >g++/gcc如何确定链接哪个库有多个版本可供选择?

g++/gcc如何确定链接哪个库有多个版本可供选择?
EN

Stack Overflow用户
提问于 2015-07-01 11:06:16
回答 1查看 2.2K关注 0票数 1

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

RHEL6上的标准设置:

代码语言:javascript
复制
$ 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++二进制文件之后,我得到:

代码语言:javascript
复制
$ 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/)

然后

代码语言:javascript
复制
$ 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

我得到了同样的..。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-01 11:14:26

GCC没有选择,就其本身而言。GCC (实际上是ld)将在链接时(通过自己的路径搜索顺序)查找和检查动态库,但它只是为库的SONAME添加了一个DT_NEEDED条目。看一看带有DYNAMICreadelf部分。

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31159545

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档