21

Clang automatically selects the gcc-version with the highest version:

$ clang++ -v main.cpp
clang version 3.8.1-12 
(tags/RELEASE_381/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9.4
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.1
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.0.1
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.4
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.1
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.2.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.0.1
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.0.1

how can i force clang to use a different gcc installation, say 5.4.1 ?

i tried to call clang with --gcc-toolchain="/usr/lib/gcc/x86_64-linux-gnu/5.4.1" but without success.

Gaetano
  • 1,090
  • 1
  • 9
  • 25
  • dupe (without an upvoted or accepted answer): http://stackoverflow.com/questions/39218360/clang-gcc-toolchain-and-gcc-4-9-3-linking-issues – bolov Feb 01 '17 at 09:00
  • 3
    it look like you need to build clang from source with `--gcc-toolchain` – bolov Feb 01 '17 at 09:01
  • 1
    @bolov building clang with --gcc-toolchain wont enable me to pick the version i want (after compilation) nor is particularly practicable – Gaetano Feb 01 '17 at 14:28
  • 1
    agreed. I was just saying that the answer there indicate there is no way to specify it the way you want. And yeah, I am pretty disappointing if there really is no way of doing it (excluding workarounds). – bolov Feb 01 '17 at 15:07

2 Answers2

15

An valid path for --gcc-toolchain is apparently "/usr" as clang seem to look for gcc in

$PREFIX/{include|lib}/gcc/$PLATFORM/$VERSION/*

so as a workaround you can trick clang to use a particular version by creating a filesystem with overlay-fs or symlinking a folder-structure containing only one folder

mkdir $MYTOOLCHAIN
cd $MYTOOLCHAIN
ln -s /usr/include include #for headerfiles
ln -s /usr/bin bin #for tools like ld
mkdir -p lib/gcc/x86_64-linux-gnu/ #clang will deduce what to select
cd lib/gcc/x86_64-linux-gnu/
#link the toolchain we want here
ln -s /usr/lib/gcc/x86_64-linux-gnu/$VERSION $VERSION 
#usage: clang++ --gcc-toolchain=$MYTOOLCHAIN main.cpp

however maybe there is a better way by instructing clang to pick the version via a flag...

Gaetano
  • 1,090
  • 1
  • 9
  • 25
  • For clang 3.8 and older, make sure that $VERSION is the full version number. For instance, if /usr/lib/gcc/x86_64-linux-gnu/5.4.1 is a link to /usr/lib/gcc/x86_64-linux-gnu/5, use "5.4.1", not "5". clang-3.9+ can deal with short toolchain versions. – proski Feb 22 '18 at 23:43
2

As of Clang-16, you can use --gcc-install-dir to simply pass the full path of the GCC installation.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157