How to Use the G++ Compiler on Linux

How To Use The Gplusplus Compiler On Linux Featured Image

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.

Also read: What to Look for in a Programming Laptop

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.

Installation

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:

sudo apt update && sudo apt install g++

On Fedora and CentOS, install it by using the yum package manager:

sudo yum install g++

You can also download it as a package from pkgs.org. This can be done using the Curl tool:

sudo curl http://ftp.de.debian.org/debian/pool/main/g/gcc-defaults/g++_10.2.1-1_amd64.deb -O

You can install the package using the dpkg utility:

sudo dpkg -i g++.deb

To check if g++ is installed, use the --version flag:

g++ --version
Gplusplus Version Check

Also read: 5 Questions to Help You Learn the Fundamentals of Programming

Basic Usage

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 --help flag:

g++ --help
Gplusplus Help Flag

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 --dumpspecs flag:

g++ --dumpspecs

This will give you a basic overview of its capabilities and different options.

To perform a basic compilation using g++, use the following format:

g++ [file]

If we have a file named “main.cpp,” for example, we can compile it by typing the following:

g++ main.cpp

The compiled executable file is named “a.out” by default.

Run it by typing the following:

./a.out

If you want to specify the name of the compiled executable file, do so by using the -o flag:

g++ -o [name] [file to compile]

Let’s say you want to specify the name of the executable file as “main.” You would type the following:

g++ -o main main.cpp
Gplusplus Compilation Result

If you want to link object files together, do so by using the following format:

g++ -o [compiled file] [obj1.o] [obj2.o]

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:

g++ -o main object-1.o object-2.o

If you want to specify a root directory, where libraries and headers can be found, use the --sysroot flag:

g++ -o [name] --sysroot [directory] main.ccp

Also read: Seven Coding Games to Help You Build Your Programming Chops

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”):

g++ -o main main.cpp -Wall

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:

g++ -o obj.o main.cpp

Next, use the ar utility with “rcs” to create an archive (“.a”) file:

ar rcs archive.a obj.o

Finally, use it with g++:

g++ -o final example.cpp archive.a

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 -c flag:

g++ -c [example.c] -o example
Gplusplus C Compilation

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.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Severi Turusenaho

Technical Writer - Linux & Cybersecurity.