public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: Patrick Herbst <paherbst@gmail.com>
Cc: gcc-help <gcc-help@gcc.gnu.org>
Subject: Re: multiple installed versions of gcc -- automatically set rpath ??
Date: Fri, 29 May 2020 18:23:03 +0100	[thread overview]
Message-ID: <CAH6eHdQOubi3nuN594av5dAaOLDcytiedO1bFYTPh5wDu53vOg@mail.gmail.com> (raw)
In-Reply-To: <CAH6eHdRRjvZSoyi6mw666_5MPDGngu4ARhTBgpWjHaktMkFcZg@mail.gmail.com>

On Fri, 29 May 2020 at 12:33, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
>
> On Fri, 29 May 2020 at 04:37, Patrick Herbst via Gcc-help
> <gcc-help@gcc.gnu.org> 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.

It looks like this works for the multilib case:

--with-specs='%{!static:%{!m32:-Wl,-rpath,/tmp/rpathinst/lib64}%{m32:-Wl,-rpath,/tmp/rpathinst/lib}}'

It assumes you know the /tmp/rpathinst prefix in advance, so isn't
perfect. I'd like it to be able to find the lib and lib64 dirs
relative to the installation dir.

      reply	other threads:[~2020-05-29 17:23 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-29  2:54 Patrick Herbst
2020-05-29  3:07 ` Dan Kegel
2020-05-29  9:28   ` Patrick Bégou
2020-05-29 11:02     ` Thomas Doczkal
2020-05-29 11:22   ` Jonathan Wakely
2020-05-29 11:33 ` Jonathan Wakely
2020-05-29 17:23   ` Jonathan Wakely [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAH6eHdQOubi3nuN594av5dAaOLDcytiedO1bFYTPh5wDu53vOg@mail.gmail.com \
    --to=jwakely.gcc@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=paherbst@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).