The compilation process is a big part of any C++ coding project. You need a compiler that is effective and simple, while also offering a variety of functionality. This is where the g++ compiler comes in. It offers a variety of functions for your compilation needs, straight from the command line. Here we show you how to get started with this great tool.
What Is g++?
G++ is best described as a C++ compiler that runs from your command line. It was released by the Free Software Foundation and is part of the GCC (GNU Compiler Collection). It turns your code, written in a high-level programming language (in this case, C++), into an executable file by transforming it into a lower-level language understood by the computer.
Most Linux installations come with g++ installed straight out of the box. However, if your distro of choice doesn’t, follow along here and learn how to install it on some of the most common distributions of Linux.
On Ubuntu and Debian, install it by using the
apt package manager:
On Fedora and CentOS, install it by using the
yum package manager:
You can also download it as a package from pkgs.org. This can be done using the Curl tool:
You can install the package using the
To check if g++ is installed, use the
Now that g++ is installed, you can start using it for your compilation needs.
Firstly, it is useful to take a look at the manual by using the
Sometimes it is useful to view extra information about the compiler and its settings. You can display the built-in spec strings of the compiler by using the
This will give you a basic overview of its capabilities and different options.
To perform a basic compilation using g++, use the following format:
If we have a file named “main.cpp,” for example, we can compile it by typing the following:
The compiled executable file is named “a.out” by default.
Run it by typing the following:
If you want to specify the name of the compiled executable file, do so by using the
Let’s say you want to specify the name of the executable file as “main.” You would type the following:
If you want to link object files together, do so by using the following format:
If, for example, you want to compile object files “object-1.o” and “object-2.o” into a “main” executable file, you would type the following:
If you want to specify a root directory, where libraries and headers can be found, use the
Using “-Wall” to Show Warning Messages
Sometimes it’s useful for your compiler to show all of the warning messages when compiling code. Luckily, g++ has this functionality built in and is ready to be used.
To show all warning messages, use the
-Wall flag (please note the uppercase “W”):
Creating a Static Library
Creating libraries is as big of a part of software development as choosing the right code editor. With a few tricks, such as the
ar command, you can easily compile a library by using g++.
Start by compiling an object file:
Next, use the
ar utility with “rcs” to create an archive (“.a”) file:
Finally, use it with g++:
Frequently Asked Questions
1. Can I use this tool to compile .c files?
Technically, you can. However, the
gcc utility is more suitable for this since g++ is primarily meant to be a C++ compiler. Additionally, g++ will treat the .c files as C++ files anyway.
If you want to use g++ to compile .c files, simply use the
2. Why shouldn’t I just use GCC as a C++ compiler?
You can very well use
gcc as a C++ compiler. However, g++ is actually an adaptation of
gcc that is more focused on C++. Thus, it offers some additional functionality and features for programmers working with C++ code.
3. What is the latest version?
The latest version of g++ seems to be 11.2.0 as of Q1 2022. It was released in July of 2021.
Our latest tutorials delivered straight to your inbox