From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io1-xd34.google.com (mail-io1-xd34.google.com [IPv6:2607:f8b0:4864:20::d34]) by sourceware.org (Postfix) with ESMTPS id 6432C386F439 for ; Fri, 29 May 2020 11:33:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 6432C386F439 Received: by mail-io1-xd34.google.com with SMTP id k18so1975580ion.0 for ; Fri, 29 May 2020 04:33:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=FS7dgICTwAziESXHNzy2GU8MarVLkOJdcLOFXopsEH0=; b=ezrYVotfZQxUrhRyC9R9Ba9Gyv6MOvjdPN3OMTSZWzY6Myhy16XmPYhJdSjyC/NnLC an84jF5hyf+5e5nsTcQPWwnFHV5vt9mOU+IRHIHNRtc+0sBfasA9whpBnrE3/l8mOBIV swUMicbpzDxCP/4wy362PJLz2USqDwIfF7QIwRi8b2FrLAqWU3W6v7h3mWcu4c+Nh84K JoWqInqYhO7PhKYl60WGdafwckBiyZPPwzrZQrJrt7RLXmR/UmeSnRxnGlULDHp5RWgE 1TK7leuunQwLAKkpkmMj8/zm4bOgPxkgNgS49JLaC4RVQ7A85uRmgll8XVCBuB/tTSHO 3bFw== X-Gm-Message-State: AOAM531i+sFf04zDM8ub0xzOoic+JihdFAiyc3bob7v1Mpv29iuO+LLs 7HLPMiPglfcYZ/FekdKJKoVGd9XpjIJcJwBGODs= X-Google-Smtp-Source: ABdhPJxWZZWUzCQPxGe7NswpXHH7mCgQCpEu0sVRn6M+H0tbUVxT22ltCKvO2Wy/0WVpReIBdBaCh1XCUny8PTbB0xg= X-Received: by 2002:a6b:c9cd:: with SMTP id z196mr6171181iof.172.1590752034822; Fri, 29 May 2020 04:33:54 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Jonathan Wakely Date: Fri, 29 May 2020 12:33:43 +0100 Message-ID: Subject: Re: multiple installed versions of gcc -- automatically set rpath ?? To: Patrick Herbst Cc: gcc-help Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 29 May 2020 11:33:57 -0000 On Fri, 29 May 2020 at 04:37, Patrick Herbst via Gcc-help 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.