问题描述
我在桌面上下载并构建了 gcc 4.8.1,运行 64 位 Ubuntu 12.04.我用源代码构建它,就像文档推荐的那样,并使用命令
I downloaded and built gcc 4.8.1 on my desktop, running 64-bit Ubuntu 12.04. I built it out of source, like the docs recommend, and with the commands
../../gcc-4.8.1/configure --prefix=$HOME --program-suffix=-4.8
make
make -k check
make install
它似乎通过了所有测试,我将所有内容都安装到我的主目录中,后缀为 -4.8,以区别于系统 gcc,即版本 4.6.3.
It seemed to pass all the tests, and I installed everything into my home directory w/ the suffix -4.8 to distinguish from the system gcc, which is version 4.6.3.
不幸的是,当我使用 g++-4.8 编译 c++ 程序时,它链接到系统 libc 和 libstdc++,而不是从 gcc-4.8.1 编译的较新的程序.我下载并构建了 gcc 4.8,因为我想在标准库中使用新的 C++11 特性,所以这种行为绝对不是我想要的.我该怎么做才能让 gcc-4.8 自动链接到它附带的标准库而不是系统标准库?
Unfortunately when I compile c++ programs using g++-4.8 it links to the system libc and libstdc++ rather than the newer ones compiled from gcc-4.8.1. I downloaded and built gcc 4.8 because I wanted to play around with the new C++11 features in the standard library, so this behaviour is definitely not what I wanted. What can I do to get gcc-4.8 to automatically link to the standard libraries that came with it rather than the system standard libraries?
推荐答案
当你用你自己的 gcc 链接时,你需要用 -Wl,-rpath,$ 添加一个额外的运行时链接器搜索路径(PREFIX)/lib64
以便在运行时找到与您的 gcc 对应的共享库.
When you link with your own gcc you need to add an extra run-time linker search path(s) with -Wl,-rpath,$(PREFIX)/lib64
so that at run-time it finds the shared libraries corresponding to your gcc.
我通常在与 gcc-4.8
和 g++-4.8
相同的目录中创建一个名为 gcc
和 g++
的包装器我调用的代码>而不是 gcc-4.8
和 g++-4.8
,如 动态链接器无法找到 GCC 库:
I normally create a wrapper named gcc
and g++
in the same directory as gcc-4.8
and g++-4.8
which I invoke instead of gcc-4.8
and g++-4.8
, as prescribed in Dynamic linker is unable to find GCC libraries:
#!/bin/bash
exec ${0}SUFFIX -Wl,-rpath,PREFIX/lib64 "$@"
安装SUFFIX
和PREFIX
时应替换为传递给configure
的内容:
When installing SUFFIX
and PREFIX
should be replaced with what was passed to configure
:
cd ${PREFIX}/bin && rm -f gcc g++ c++ gfortran
sed -e 's#PREFIX#${PREFIX}#g' -e 's#SUFFIX#${SUFFIX}#g' gcc-wrapper.sh > ${PREFIX}/bin/gcc
chmod +x ${PREFIX}/bin/gcc
cd ${PREFIX}/bin && ln gcc g++ && ln gcc c++ && ln gcc gfortran
(gcc-wrapper.sh
是那个 bash 片段).
(gcc-wrapper.sh
is that bash snippet).
上述解决方案不适用于某些版本的 libtool
,因为 g++ -Wl,... -v
假定链接模式并失败并出现错误.
The above solution does not work with some versions of libtool
because g++ -Wl,... -v
assumes linking mode and fails with an error.
更好的解决方案是使用规范文件.构建 gcc/g++ 后,调用以下命令使 gcc/g++ 添加 -rpath
到链接器命令行(根据需要替换 ${PREFIX}/lib64
):
A better solution is to use specs file. Once gcc/g++ is built, invoke the following command to make gcc/g++ add -rpath
to the linker command line (replace ${PREFIX}/lib64
as necessary):
g++ -dumpspecs | awk '/^*link:/ { print; getline; print "-rpath=${PREFIX}/lib64", $0; next } { print }' > $(dirname $(g++ -print-libgcc-file-name))/specs
这篇关于将 g++ 4.8 链接到 libstdc++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!