Fedora

How to Install G++ to Compile C++ on Fedora Linux

G++ or GNU C++ is the CLI compiler for C++ which you can use on your Linux, macOS, and Windows system. It is a widely used C++ compiler that offers a simple way to turn the C++ code into executable programs.

G++ offers different options to manage the compilation process according to the requirements. Furthermore, G++ supports different C++ standards which makes it an essential utility in the development ecosystem.

If you also want to know how to install G++ to compile C++ on Fedora Linux, this tutorial is for you. Here, we will explain a simple method to install G++ without hassle.

How to Install G++ on Fedora Linux

Let’s divide this section into multiple parts to describe the various ways to install G++ and then use it to compile C++ on Fedora 38.

Using the Dnf Package Manager

Run the following command to update the Fedora Linux according to the latest one available:

sudo dnf update

Now, run the following command to install the G++ package:

sudo dnf install gcc-c++

Once you install G++, check its version through the following command:

g++ --version

Using the Developer Tools

You can install the Developer Tools group which includes G++. Run the following command to install it:

sudo dnf group install "Development Tools"

Using the RPM Fusion Repository

RPM Fusion is a third-party software repository which provides additional Fedora Linux packages. Open a terminal and run the following commands to enable the repository:

sudo dnf install -y https://mirrors.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm

Once the repository is enabled, run the following command to install G++:

sudo dnf install gcc-c++

How to Use G++ to Compile C++ on Fedora

Let’s take an example where we create a C++ code and execute it through G++. First, execute the following command to create a C++ file:

touch c++.cpp

Here, we enter the following C++ code to print the “Linux World” as an output:

#include <iostream>

int main() {

std::cout << "Linux World" << std::endl;

return 0;

}

Now, you need to change the executable permission for the file through the following command:

chmod u+x c++.cpp

Once you are done, execute the following given command to compile the C++ program:

g++ -o Linux c++.cpp

In the previous command, the -o option and Linux specify the filename output for the program. Finally, run the following command to execute C++ through G++:

./Linux

Conclusion

This is all about how you can install and use G++ to compile C++ on Fedora Linux. With G++ on the Linux system, you can compile the C++ programs right from the terminal. Moreover, we included multiple methods to install the G++ utility in the system easily. We also recommend you to use the correct RPM repos. Otherwise, you will face various errors during installation.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.