I am trying to find the highest version of the C & C++ compiler installed on the OS.
I'm trying to optimize my code instead of taking up 25 lines and rewriting a bunch of if which
commands over and over.
The script can be accessed by anyone on any version of distros and so the list can be long and full of possibilities and I want the latest version to run the build.
The reason I am posting this is because I keep getting back the match gcc-11
when gcc-12
is installed and shows up when manually running which gcc-12
... so why is it only matching a lower version?
#!/bin/bash
clear
c_compilers=(gcc-13 gcc-12 gcc-11 gcc-10 gcc-9 gcc clang-16 clang-15 clang-14 clang-13 clang-12 clang-11 clang-10 clang)
cxx_compilers=(g++-13 g++-12 g++-11 g++-10 g++-9 g++ clang++-16 clang++-15 clang++-14 clang++-13 clang++-12 clang++-11 clang++-10 clang++)
for i in ${c_compilers[@]}
do
cc_match="$(which $i)"
if [ -n "$cc_match" ]; then
CC="$cc_match"
break
fi
done
for i in ${cxx_compilers[@]}
do
cxx_match="$(which $i)"
if [ -n "$cxx_match" ]; then
CXX="$cxx_match"
break
fi
done
echo $PATH
echo $CC
echo $CXX