且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将g ++ 4.8链接到libstdc ++

更新时间:2023-11-10 17:40:58

当你链接到自己的gcc,你需要添加一个额外的运行时链接器搜索路径 -Wl,-rpath,$(PREFIX)/ lib64 ,以便在运行时找到与您的gcc对应的共享库。



我通常在与相同的目录下创建一个名为 gcc g ++ 我调用了gcc-4.8 g ++ - 4.8 ,而不是 gcc-4.8 code> g ++ - 4.8 ,如动态链接器无法查找GCC库

 #!/ bin / bash 
exec $ {0} SUFFIX - Wl,-rpath,PREFIX / lib64$ @

安装 SUFFIX PREFIX 应替换为传递给 configure

  cd $ {PREFIX} / bin&& rm -f gcc g ++ c ++ gfortran 
sed -e'##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






上述解决方案不适用于某些版本的 libtool ,因为 g ++ -Wl,... -v 假定为链接模式,并且失败并显示错误。


$ b b

更好的解决方案是使用specs文件。一旦gcc / g ++被构建,调用以下命令使gcc / g ++添加 -rpath 到链接器命令行(替换 $ {PREFIX} / lib64 ):

  g ++ -dumpspecs | awk'/ ^ \ * link:/ {print; getline; print-rpath = $ {PREFIX} / lib64,$ 0; next} {print}'> $(dirname $(g ++ -print-libgcc-file-name))/ specs 


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

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.

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?

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.

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 "$@"

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 is that bash snippet).


The above solution does not work with some versions of libtool because g++ -Wl,... -v assumes linking mode and fails with an error.

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