How to Use the G++ Compiler on Linux and How to Run First Program?


The G++ compiler is a useful tool for programmers who use Linux computers to build and run C++ programs. In this piece, we’ll show you step-by-step how to use the G++ compiler, from setting it up to using its more advanced features.

The G++ engine is a part of the GNU engine Collection (GCC), and many Linux users use it to write C++ programs. It has all the tools and improvements you need to write good machine code.

A big part of any C++ job is the process of building. You need a processor that works well, is easy to use, and gives you a lot of choices. The G++ engine makes this easy. You can do a lot of different things with compilation from the command line. Here, we’ll show you how to use this great tool for the first time.

What is G++?

G++ is best thought of as a C++ program that is run from the command line. It came from the Free Software Foundation and is part of the GNU Compiler Collection (GCC). It changes your code, which was written in a high-level programming language (in this case, C++), into a lower-level language that the computer can understand.

Installation of G++ on Linux

Most versions of Linux come with G++ already set up. Read on to learn how to add it to some of the most popular Linux distributions if it’s not already included in your favorite Linux distribution.

On Ubuntu and Debian, use the apt package manager to set it up:                            sudo apt update && sudo apt install g++

You can set it up on Fedora and CentOS with the Yum package manager:
sudo yum install g++

You can also get it from as a package. Using the Curl tool, you can do this: sudo curl -O

The dpkg tool can be used to load the package:                                                      sudo dpkg -i g++.deb

To check if g++ is loaded, use the –version flag:                                                          g++ –version

How to Write Your First Program in G++ on Linux?

Before we learn how to build, let’s make a simple program that says “Hello, World!” Open a text tool and type the following code into it:

How to Use the G++ Compiler on Linux

#include <iostream>

       int main() {
            std::cout << “Hello, World!” << std::endl;
            return 0;

Using the cpp extension make a copy of the #include iostream> code. Save the file with an ending like “hello.cpp.”

Compiling and Running the Program

To run the C++ program with the G++ compiler, open a terminal and go to the place where you saved the hello.cpp file. To put the program together, use the following command:

g++ hello.cpp -o hello

How to Use the G++ Compiler on Linux

This program tells the G++ compiler to turn the code in hello.cpp into an executable file called hello. Type the following instructions to start the app:

Copy the code to the./hello file.

“Hello, World!” should be written on the screen.

Understanding the Compiler Options

There are many choices in the G++ engine that let you change how the program is compiled and turn certain features on or off. Here are a few of the most popular options:

  • o “output”: Specifies the name of the file that will be made.
  • Wall: Turn on more warning signs while the assembly is going on.
  • O2: Level 2 lets you get better.
  • std=c++11 makes C++11 the language standard.

You can find out about more compiler choices in the documentation for the G++ translator.

How to Use the G++ Compiler on Linux

Using Standard Libraries

C++ comes with a lot of standard libraries that have methods and classes for common jobs that are ready to use. When you use the G++ engine to make a program, these tools are automatically linked for you. For example, you can add the following line to your program to use the std::.vectors from the vector library stored in a container:

#include <vector>

If you need to use certain features, make sure your program is built with the right standard library choices.

Debugging with GDB

GDB, which stands for “GNU Debugger,” is a very helpful tool for fixing bugs in G++ programs. You can set breakpoints, step through the code, and look at the variables to find problems and fix them. Use the following command to find problems in a program made with the G++ engine:

Copy code: g++ -g hello.cpp -o hello_debug

The -g option, which is part of this command, adds debugging details to the file. Then, run the following command to start the GDB debugger:

Copy code gdb hello_debug

Optimizing Compilation

With the G++ engine, you can speed up your C++ programs in a number of ways. These choices can reduce the size of the executable, speed up how fast it runs, and let you make specific changes based on the platform you’re targeting. Try different optimization levels (-O1, -O2, and -O3) to find the best mix between speed and compile time.

Advanced Compilation Features

The G++ program can do more than just the basic compilation method. Script directions, templates, namespaces, and more are some of these features. You can use these tools to write C++ code that can be changed and works well. This book doesn’t talk about these topics, but you can find out more about them in the official G++ compiler directions.


When you use the G++ engine, it’s easy and safe to build and run C++ programs on Linux. We talked about how to install, how to write a simple program, how to build, how to understand compiler options, how to use standard libraries, how to fix with GDB, how to improve compilation, and a little bit about more advanced features of compilation. Now that you know these things, you can start to learn more about writing in C++ on Linux. To read more content like this, visit


Q1: Can operating systems other than Windows use the G++ engine?

Yes, the G++ engine can be used on Linux, macOS, Windows, and other computers.

Q2: How do I get the most recent version of the G++ compiler?

You can load the most recent version of the G++ compiler by updating the package manager on your Linux machine and running the command for your Linux distribution.

Q3: Are there any other tools besides the G++ engine for writing C++ code?

Yes, you can use Clang and the Intel C++ Compiler or other C++ processors. Each one has its own jobs and ways to do better at them.

Q4: Can I use the G++ engine to put together more than one code file?

Yes, you can join more than one source file by putting the names of the source files in the assembly command with spaces between them.

Q5: Can I write C code with the G++ engine?

Yes, you can use the G++ engine to write in both C and C++. C programs can also be made with it.

We looked at how to use the G++ engine on Linux in this article. You now know how to build your own C++ programs from scratch and run them on Linux. You also know how to use the more powerful tools that come with the program. Enjoy making code!

Leave A Reply

Your email address will not be published.