Linking g++ 4.8 to libstdc++

后端 未结 3 2034
长发绾君心
长发绾君心 2020-11-28 12:51

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

../../g         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 13:31

    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
    

提交回复
热议问题