How to Use the G++ Compiler on Linux (2024)

How to Use the G++ Compiler on Linux (1)

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.

Content

  • What Is g++?
  • Installation
  • Basic Usage
  • Using "-Wall" to Show Warning Messages
  • Creating a Static Library
  • Frequently Asked Questions

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:

How to Use the G++ Compiler on Linux (2)

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
How to Use the G++ Compiler on Linux (3)

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
How to Use the G++ Compiler on Linux (4)

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
How to Use the G++ Compiler on Linux (5)

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

How to Use the G++ Compiler on Linux (6)

Severi Turusenaho

Technical Writer - Linux & Cybersecurity.

  • Facebook
  • Tweet
How to Use the G++ Compiler on Linux (2024)

FAQs

What is the g++ command in Linux? ›

G++ is a specialized front end for the gcc compiler. Gcc compiles c code. If you can't find what you want in g++, try man gcc. Most c and many c++ functions also have man pages, so you should get comfortable using them.

How to run g++ in terminal? ›

To run the C++ program in your terminal, you need to make sure to change the directory to the one where your program resides, you can use the 'cd <File_Path>' command to change the path and to generate an executable file, the command, 'g++ File_Name. cpp -o FileName', is used.

What compiler should I use for C++ on Linux? ›

Although you'll use VS Code to edit your source code, you'll compile the source code on Linux using the g++ compiler. You'll also use GDB to debug.

How does g++ work? ›

G++ is a compiler, not merely a preprocessor. G++ builds object code directly from your C++ program source. There is no intermediate C version of the program. (By contrast, for example, some other implementations use a program that generates a C program from your C++ source.)

How to check G ++ version in Linux? ›

You can do that by checking the output of the various build commands and/or by running gcc --version .

Are GCC and g++ the same? ›

GCC and G++ are both used for compiling C and C++ languages. However, GCC is used for compiling C programs while G++ is used for compiling C++ programs. G++ automatically links files in the libraries of std C++ during object file linking, which does not happen with GCC.

What is the name of the compiler G ++? ›

The fine folks in the GNU project have created a C/C++ compiler that has been ported to many different architectures. The name of the compiler is gcc or g++ for the C or C++ compiler respectively.

Can you run C++ on Linux? ›

With Linux you can program in some of the most important languages on the planet, such as C++. I'll demonstrate how this is done on both Ubuntu and Red Hat distributions on how to write and compile all from the command line. To run it as a command, issue the command: g++ -o hello hello.

What's the difference between g++ and gcc? ›

The GCC compiler is used to compile the C programs, whereas the G++ is used for C++ programs. While you could still use GCC to compile C++ program (by using it in gcc -xc++ -lstdc++ -shared-libgcc format, g++ provides a simpler syntax. Actually, GCC (GNU Compiler Collection) is a super set of various compilers.

What is wall g++? ›

-Wall show all warnings. It turns on all standard C++ warnings about code that might cause unexpected or undefined behavior. -g provides debugging feature for your program.

Where does g++ search for libraries? ›

g++ does find the library in /usr/local/lib : once you do things properly and actually tell it the name of the library to look for.

Top Articles
Latest Posts
Article information

Author: Gov. Deandrea McKenzie

Last Updated:

Views: 6722

Rating: 4.6 / 5 (66 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Gov. Deandrea McKenzie

Birthday: 2001-01-17

Address: Suite 769 2454 Marsha Coves, Debbieton, MS 95002

Phone: +813077629322

Job: Real-Estate Executive

Hobby: Archery, Metal detecting, Kitesurfing, Genealogy, Kitesurfing, Calligraphy, Roller skating

Introduction: My name is Gov. Deandrea McKenzie, I am a spotless, clean, glamorous, sparkling, adventurous, nice, brainy person who loves writing and wants to share my knowledge and understanding with you.