Installing C++ Compiler on UNIX/Linux

Installing C++ Compiler on UNIX/LinuxInstalling a C++ compiler (usually <span>g++</span> or <span>clang++</span>) on UNIX/Linux systems is the first step in C++ development. As a typical UNIX-like system, most Linux distributions natively support or can easily install C++ compilers. Below, I will detail how to install a C++ compiler on popular Linux distributions (such as Ubuntu, Debian, CentOS, Fedora, Arch, etc.), as well as how to verify the installation and compile and run your first C++ program.

🎯 1. What is a C++ Compiler (Review)

A C++ compiler is a tool that compiles the C++ source code you write (for example, <span>main.cpp</span>) into an executable program. The most commonly used C++ compilers on Linux/UNIX systems are:

Compiler Description Command
GCC / G++ GNU Compiler Collection, the most popular, supports multiple languages, <span>g++</span> is used for compiling C++ <span>g++</span>
Clang / Clang++ Produced by LLVM, high performance, diagnostic-friendly, gradually becoming the mainstream choice <span>clang++</span>

✅ 2. Methods to Install C++ Compiler on Popular Linux Distributions

🐧 1. Ubuntu / Debian and their derivatives (such as Linux Mint, Pop OS)

✅ Install G++ (GNU C++ Compiler)

Open the terminal (Ctrl + Alt + T) and run the following command:

sudo apt update
sudo apt install g++



✅ (Optional) Install Clang / Clang++

If you want to try the Clang compiler (recommended for stricter code checks or as an alternative to GCC):

sudo apt install clang



Clang++ is the C++ compiler for Clang, usually installing <span>clang</span> will also install <span>clang++</span>

✅ Verify Installation

Check if the installation was successful:

g++ --version



or

clang++ --version



You should see output similar to the following, displaying the compiler version information:

g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.



✅ Compile and Run Your First C++ Program

  1. Create a simple C++ file, for example, <span>main.cpp</span>:
nano main.cpp



  1. Input the following code:
#include<iostream>
using namespace std;

int main() {
    cout << "Hello, Linux C++!" << endl;
    return 0;
}



Press <span>Ctrl + O</span> to save, <span>Ctrl + X</span> to exit the editor.

  1. Compile the code:
g++ main.cpp -o main



This command means: compile <span>main.cpp</span> with g++, generating an executable file named <span>main</span>

  1. Run the program:
./main



🎉 If the output is:

Hello, Linux C++!



It indicates that your C++ environment has been successfully configured!

🐂 2. CentOS / RHEL / Fedora

✅ CentOS / RHEL 7 / 8 / 9

Use the <span>yum</span> or <span>dnf</span> package manager (recommended to use <span>dnf</span> after CentOS 8)

# Install G++
sudo yum install gcc-c++        # CentOS 7
sudo dnf install gcc-c++        # CentOS 8 / RHEL 8 / Rocky Linux / AlmaLinux



✅ Fedora

sudo dnf install gcc-c++



✅ Install Clang (Optional)

sudo dnf install clang



✅ Verify:

g++ --version



🦀 3. Arch Linux / Manjaro

The package manager for Arch is <span>pacman</span>, which is very straightforward.

✅ Install G++

sudo pacman -S gcc



✅ Install Clang

sudo pacman -S clang



✅ Verify:

g++ --version



or

clang++ --version



🐧 4. openSUSE

Use the <span>zypper</span> package manager:

sudo zypper install gcc-c++



Or install Clang:

sudo zypper install clang



✅ 3. How to Choose a Compiler: G++ or Clang++?

Feature G++ (GCC) Clang / Clang++
Developer GNU Project LLVM Project
Default Compiler (Most Distributions) ✅ Yes (e.g., Ubuntu, CentOS) ❌ Usually needs to be installed separately
Compilation Speed Average Usually faster, especially for incremental compilation
Error Messages / Diagnostic Information Average ✅ Clearer and more user-friendly, suitable for learning
Standard Support Very good Very good, sometimes more cutting-edge
Use Cases General-purpose, good compatibility Modern C++, focus on code quality, IDE integration

Recommendation: For beginners, either can be used, but it is recommended to start with <span>g++</span>, and if you want better error messages, you can try <span>clang++</span>

✅ 4. Common Compilation Commands

Once you have installed <span>g++</span> or <span>clang++</span>, you can use the following commands to compile C++ programs:

Basic Compilation Command:

g++ source_file.cpp -o output_file_name



For example:

g++ main.cpp -o main



  • This will compile <span>main.cpp</span> into an executable file named <span>main</span>.
  • Then run it:
./main



With Debug Information (for gdb debugging):

g++ -g main.cpp -o main



Enable Optimization (e.g., O2):

g++ -O2 main.cpp -o main



✅ 5. Summary: Installing C++ Compiler on UNIX/Linux

Step Action
1. Confirm System Type e.g., Ubuntu, CentOS, Fedora, Arch, etc.
2. Open Terminal Use shortcut Ctrl + Alt + T
3. Update Package List (Recommended) <span>sudo apt update</span> (for Ubuntu) or corresponding command
4. Install G++ (Recommended) <span>sudo apt install g++</span> (for Ubuntu), etc.
5. (Optional) Install Clang <span>sudo apt install clang</span>
6. Check Installation Success <span>g++ --version</span> or <span>clang++ --version</span>
7. Write C++ Code e.g., <span>main.cpp</span>
8. Compile Code <span>g++ main.cpp -o main</span>
9. Run Program <span>./main</span>

❓ 6. Frequently Asked Questions

❓ Q1: Is a C++ compiler installed by default on Linux?

  • Most modern Linux distributions (like Ubuntu, Fedora) may not have <span>g++</span> installed by default, but it is definitely available in the package manager for installation at any time
  • You can check if it is installed by running <span>g++ --version</span>

❓ Q2:<span>gcc</span> and <span>g++</span> what is the difference?

Command Function
gcc GNU C Compiler, used to compile C language code
g++ GNU C++ Compiler, used to compile C++ code, automatically links the C++ standard library (like <span>iostream</span>)

⚠️ Although <span>gcc</span> can also compile <span>.cpp</span> files, it will not automatically link the C++ standard library, which may lead to linking errors, therefore it is recommended to always use <span>g++</span> to compile C++ programs

❓ Q3: Can I use Visual Studio on Linux?

  • You cannot run Visual Studio directly (Windows application)
  • But you can:
    • Use Visual Studio Code (VS Code) (cross-platform, free, supports Linux)
    • Combine with g++ / clang++ and plugins to write and run C++ programs

✅ 7. Recommended Next Steps

Goal Recommended Action
Write and run your first C++ program Use <span>g++ main.cpp -o main && ./main</span>
Learn C++ basic syntax Start with Hello World, gradually learn variables, functions, classes, etc.
Use IDE / Editor Recommended: VS Code (cross-platform, rich plugins) or CLion (professional C++ IDE)
Learn Build Tools Such as Makefile or CMake, for managing complex projects
Install Debugging Tools Such as <span>gdb</span>, to debug programs with the <span>-g</span> compile option

Leave a Comment