1

our server have different gcc version, and sometime we need to swtich different gcc version base on different project.

And I don't want to hard code the gcc in my bashrc,

so I want to write a bash function to switch different GCC version while using build tool like:

#gcc4
export CXX4="/usr/local/brion_AVX/gcc/current/bin/c++"
export CC4="/usr/local/brion_AVX/gcc/current/bin/gcc"
export GCC4="/usr/local/brion_AVX/gcc/current/bin/g++"

#gcc7
export CXX7="/usr/local/brion_AVX/gcc/7.3.1_3/bin/c++"
export CC7="/usr/local/brion_AVX/gcc/7.3.1_3/bin/gcc"
export GCC7="/usr/local/brion_AVX/gcc/7.3.1_3/bin/g++"

#gcc12
export CXX12="/group/DEV/GUI/SHARED/gcc/12.2.0/bin/c++"
export CC12="/group/DEV/GUI/SHARED/gcc/12.2.0/bin/gcc"
export GCC12="/group/DEV/GUI/SHARED/gcc/12.2.0/bin/g++"

gcc_switch()
{
    g1=CXX$1
    g2=CC$1
    g3=GCC$1
    export CXX=$g1
    export CC=$g2
    export GCC=$g3
    echo "switch gcc to $CXX"
}

then when I run "gcc_switch 12", it switch to gcc 12, and export GCC as gcc12 path,

But it doesn't, seem CXX12 will just a string, the CXX=$g1 doesn't get the real value of g1.

I can write the "gcc_swtich" function and work expect using if condition like:

gcc_switch()
{
    if [[ $1 == 4 ]]; then
        export CXX=$CXX4
        export CC=$CC4
        export GCC=$GCC4
    elif [[ $1 == 7 ]]; then
        export CXX=$CXX7
        export CC=$CC7
        export GCC=$GCC7
    elif [[ $1 == 12 ]]; then
        export CXX=$CXX12
        export CC=$CC12
        export GCC=$GCC12
    fi

    echo "switch gcc to $CXX"
}

But it doesn't seems elegant and flexible, is it posible to make the frist gcc_switch function works without using if in bash function with using map?

bbrown
  • 41
  • 4

0 Answers0