Command line gcc -v and g++ -v output inconsistent versions
Foreword: This article was first edited on January 30, 2024
CSDN Home Page:/rvdgdsva
Blogland Home Page:/hassle
My bug description: There is a default environment base and a created environment your_env_name in the conda environment. gcc7.5 and g++7.5 are installed in the base environment, and gcc14 and g++14 are installed in the created environment. the output versions of the commands gcc -v and g++ -v in the virtual environment are not the same.
In the virtual environment, run the following code, which shows gcc version 14.0
(your_env_name) XiaoMing@LAPTOP:~$ gcc -v
//Results
gcc version 14.1.0 (conda-forge gcc 14.1.0-1)
Running the following code shows that the g++ version is 7.5
(your_env_name) XiaoMing@LAPTOP:~$ g++ -v
//Results
gcc version 7.5.0 (Ubuntu 7.5.0-6ubuntu2)
rationale
Went looking for the error message and saw this
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/home/XiaoMing/miniconda3/envs/your_env_name/bin/
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
There are two different GCC compiler installations on the system at the same time: one by the Miniconda3 environmentyour_env_name
GCC version 14.1.0 is provided, and the other is the GCC 7 version that comes with the system.gcc -v
The command shows the Miniconda3 environmentyour_env_name
The GCC compiler information configured in theg++ -v
command displays information about the system's default g++ compiler.
gcc -v
in the outputTarget: x86_64-conda-linux-gnu
specifies the compiler target architecture in the Miniconda environment, and theg++ -v
in the outputTarget: x86_64-linux-gnu
Specifies the target architecture of the system default compiler. This indicates that the GCC compiler in the Miniconda environment is different from the system default g++ compiler.
The reason for this discrepancy could be your system environment variablesPATH
setting causes the shell to prioritize searching the Miniconda environment for thegcc
but (not)g++
then it still points to the system's default compiler. Alternatively, the system may have multiple versions of the GCC compiler installed at the same time, and thegcc
cap (a poem)g++
The symbolic links point to different versions.
deal with
Open a terminal and enter the following command to view the currentPATH
Environment variables:
echo $PATH
This will output a file that starts with a colon (:
) separated by a list of directories. These directories are the paths where the shell searches for executables.
in the outputPATH
in the Miniconda environmentyuan
(used form a nominal expression)bin
directory path (e.g./home/XiaoMing/miniconda3/envs/your_env_name/bin
) and the path to the system's default GCC compiler (e.g./usr/bin
maybe/usr/local/bin
)。
Find your shell configuration file. This is usually~/.bashrc
(bash shell), ~/.zshrc
(zsh shell), or other similar file. Open the file in a text editor (e.g.nano ~/.bashrc
maybevim ~/.bashrc
), but I would recommend usingnano ~/.bashrc
. Find in the filePATH
variable's definition line and modify its order to place the system default GCC path before the Miniconda path. For example, if yourPATH
Definitions are as follows:
export PATH="/home/youmu/miniconda3/envs/yuan/bin:$PATH"
Modified to:
export PATH="/usr/bin:/home/youmu/miniconda3/envs/yuan/bin:$PATH" # or other path containing the system GCC paths
This step is designed to adjust thePATH
The order of the environment variables so that the system searches for thegcc
command, it prioritizes the system's default path to thegcc
instead of the Miniconda environment'sgcc
Save and close the file. Then run the following command to make the changes take effect:
source ~/.bashrc # or source ~/.zshrc etc.
re-rungcc -v
cap (a poem)g++ -v
command to see if the output has changed.
Unsurprisingly, the gcc in your virtual environment will change from pointing to the compiler in the Miniconda environment (your_env_name) to pointing to the system default (base) compiler.