[linux] Can’t tell the difference between gcc/g++? How to implement multi-version gcc/g++ switching in ubuntu

How to switch between different versions of gcc/g++ on the same server at will?

1. Distinguish between gcc/g++

  1. What is gcc/g++

GCC: GNU Compiler Collection (GUN compiler collection), which can compile C, C++, JAV, Fortran, Pascal, Object-C, Ada and other languages.
gcc is the GUN C Compiler (C compiler) in GCC
g++ is the GUN C++ Compiler (C++ compiler) in GCC

  1. What is the difference between gcc/g++?
  1. For .c and .cpp files, gcc compiles them as c and cpp files respectively (the syntax strength of c and cpp is different)
  2. For .c and .cpp files, g++ compiles them uniformly as cpp files.
  3. When using g++ to compile a file, g++ will automatically link the standard library STL, but gcc will not automatically link the STL
  4. When gcc compiles C files, there are relatively few predefined macros that can be used.
  5. When gcc compiles cpp files/g++ compiles c files and cpp files (at this time, gcc and g++ call the compiler of cpp files), some additional macros will be added. These macros are as follows:
    #define GXX_WEAK 1
    #define __cplusplus 1
    #define __DEPRECATED 1
    #define GNUG 4
    #define __EXCEPTIONS 1
    #define private_extern extern
  6. When compiling c++ files with gcc, in order to be able to use STL, you need to add the parameter –lstdc++, but this does not mean that gcc –lstdc++ is equivalent to g++. Their difference is not only this

2. Check and install gcc/g++ version

  1. Check the current gcc/g++ version
gcc -v
g++ -v

Insert image description here
You can see that the version I have here is gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~16.04)

  1. Check the gcc/g++ version installed on the system
ll /usr/bin/gcc*
ll /usr/bin/g++*

Insert image description here

  1. If the required version is not available, install it first

For example:

sudo apt install gcc-5
sudo apt install g++-5

#安装gcc-7和g++-7
sudo apt install gcc-7
sudo apt install g++-7

If the installation fails, refer to this article to solve the problem: [ubuntu] apt-get install failed solution and parameter analysis

3. Use update-alternatives to manage version switching

1. First check whether the current gcc/g++ is managed through update-alternatives

See how it's linked

ll /usr/bin/gcc*
ll /usr/bin/g++*

As shown in the figure below, it indicates that it is managed by update-alternatives.
Insert image description here
As shown in the figure below, it indicates that it is not managed by update-alternatives, but is directly linked to the executable file of the corresponding version.
Insert image description here

2. First add the required version to update-alternatives

Method 1: Add gcc/g++ separately

# gcc
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 50
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70

# g++
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 50
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 70
# 数字50,70代表优先级,数字越大优先级越高

If an error is reported: update-alternatives: error: alternative g++ can't be master alternative: it is a slave of gcc.
Then you have to use method 2

Method 2, add gcc/g++ together

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70 --slave /usr/bin/g++ g++ /usr/bin/g++-7

If an error is reported: update-alternatives: error: alternative g++ can't be slave of gcc: it is a master alternative.
Then method 1 must be used.

### 3. 手动配置update-alternatives选择gcc/g++版本

```bash
sudo update-alternatives --config gcc
sudo update-alternatives --config g++

When the prompt shown in the figure below appears, enter the corresponding number to switch. Currently it is gcc-7. If you want to switch to gcc-6, enter 1 and press Enter.
Insert image description here
Then enter gcc -v to view the version.

You can see that the modification was successful, changing from 7 to 6, gcc version 6.5.0 20181026 (Ubuntu 6.5.0-2ubuntu1~16.04)
Insert image description here

3. Remove from update-alternatives

sudo update-alternatives --remove gcc /usr/bin/gcc-5

4. Switch versions directly through soft links

1. Let’s check the linking method of gcc

ll /usr/bin/gcc*

You can see that blue represents the connection file and green represents the executable file.
In fact, when different versions of gcc/g++ are called, the green executable file of the corresponding version is ultimately called.
For example:
the executable file called by gcc-6 is x86_64 -linux-gnu-gcc-6
The executable file called by gcc-7 is x86_64-linux-gnu-gcc-7.
Insert image description here
Therefore, if you want to modify the system's default gcc/g++ version, you only need to directly pass the executable file of the corresponding version through Just connect the soft link to the /usr/bin/gcc file.

2. Soft link switching version

2.1 Delete the original soft link

method one:

sudo unlink /usr/bin/gcc

Method 2: Be careful not to type too many '/'s, otherwise the actual data will be deleted

sudo rm -rf /usr/bin/gcc

2.2 Create a new soft link

sudo ln -s /usr/bin/x86_64-linux-gnu-gcc-7 /usr/bin/gcc

Check the results and find that the link is successful.
Insert image description here
Then enter gcc -v and you can see that the modification is successful.
Insert image description here

5. Summary

In fact, it was found that versions three and four essentially achieve version switching by changing soft links. The third method is to first unifiedly manage different soft links through update-alternatives to facilitate switching. The fourth is to switch directly by modifying the soft link, which is more direct.

learn by analogy

In fact, update-alternatives can be used to switch versions of many programs, such as cuda

  • First add to update-alternatives
sudo update-alternatives --install /usr/local/cuda cuda /usr/local/cuda-10.0 10
sudo update-alternatives --install /usr/local/cuda cuda /usr/local/cuda-10.2 20

Switch version again

sudo update-alternatives --config cuda

Insert image description here
Enter the number to switch the version, and you will find that you can switch successfully.
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_43693967/article/details/123719406#comments_28551687