Re: multiple installed versions of gcc -- automatically set rpath ??

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 29 May 2020 at 04:37, Patrick Herbst via Gcc-help
<gcc-help@xxxxxxxxxxx> wrote:
>
> I have the base gcc that came with my distro and i'd like to have the
> option of using a newer version without replacing the older one.
>
> I'd like to install the newer version in a different path, like /opt.
>
> I run into a problem though when compiling with the newer version in
> /opt, it links against the libstdc++ in /opt, but at runtime the
> executable tries to link with the systems version in /usr/lib.
>
> Is there a way to have the gcc in /opt automatically set the rpath
> when linking so that executables can run against the /opt libstdc++?


The simplest solution is to just use a shell script/function/alias to
invoke the new GCC and have that automatically add the -Wl,-rpath
flags. For example, I use this bash function:

GCC ()
{
    local id=$1;
    local version=${id%/*};
    shift;
    local dir=$HOME/gcc/${version};
    local lib=lib64;
    for arg in "$@";
    do
        case $arg in
            -m32)
                lib=lib
            ;;
            -m64)
                lib=lib64
            ;;
        esac;
    done;
    local libdir=${id/$version/$dir\/$lib};
    local colour=-fdiagnostics-color;
    if [[ $version =~ 4.[12345678] ]]; then
        colour='';
    fi;
    ( set -o pipefail;
    LANG=C $dir/bin/g++ -Wall -Wextra -g "${@:--v}"
${@:+-Wl,-rpath,$libdir} $colour 2>&1 | less -FR )
}

so that "GCC N ..." can be used to run ~/gcc/N/bin/g++ and set the
rpath to ~/gcc/N/lib64 or ~/gcc/N/lib as appropriate. Then I haveshell
aliases using that:

g++14 is aliased to `GCC latest -std=gnu++14'
g++17 is aliased to `GCC latest -std=gnu++17'

where ~/gcc/latest is a symlink to (currently) ~/gcc/11

That shell function is overkill for most people (I have nearly 100 GCC
builds under ~/gcc which is unusual!) but the general idea of a
shortcut to invoke the new compiler with the -Wl,rpath option works
well.


You can also use a custom specs file, as shown in the stackoverflow
link Dan Kegel gave (but ignore the answer there about
--with-boost-ldflags as that's wrong). The answer showing how to use a
specs file only works for non-multilib compilers though. If you want
to be able to link both 32-bit and 64-bit code you'll need to make the
link specs smarter, to adjust it based on the presence/absence of the
-m64 or -m32 flags.



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux