Advertisement
Tech

G++ Linux Terminal Source Code Compiler

For many studying a programming language the tools can be very costly. Fortunately Linux has some outstanding tools to help you compile your applications. In this entry to the Brighthub Linux Command Line series you will learn how to compile C++ with the help of g++.

By jlwallen
Desk Tech
Reading time 3 min read
Word count 448
Linux Computing Linux commands
G++ Linux Terminal Source Code Compiler
Advertisement
Quick Take

For many studying a programming language the tools can be very costly. Fortunately Linux has some outstanding tools to help you compile your applications. In this entry to the Brighthub Linux Command Line series you will learn how to compile C++ with the help of g++.

On this page

What is g++

The g++ compilation tool is a part of the gcc suite that is used to compile both C and C++ code. The g++ command line does preprocessing, complation, assembly, and linking. The g++ command is very easy to use and does its job much faster than any GUI alternative. Using g++ is a great alternative for those who need a free alternative to Microsoft Studio applications.

Basic Usage

First and foremost you must know that g++ has a TON of options and switches. So many it can be rather overwheming. To avoid scaring anyone from using g++, I will stick with simplicity and illustrate how to compile a simple “hello world” C++ code. This will serve to illustrate how simple compiling with g++ can be.

Advertisement

With that said let’s take a look at compilation. As I said we will use a simple “Hello World” snippet of code. Here’s the code:

#include

Advertisement

int main()

{

Advertisement

std::cout « “Hello Brighthub!\n”;

}

Advertisement

This file will be called test.cpp.

With that file in place the command:

Advertisement

g++ -o test test.cpp

This will compile, assemble, and link the application. The “-o” switch instructs g++ to place the output of the compilation into the file that follows “-o”. This output will effectively be the name of the compiled application, which will in turn be the binary application created by the compilation. In order to run the application you will need to change the permissions of the file with the command:

Advertisement

chmod u+x test

To run this newly created application you only need to issue the command (from within the directory housing the file) ./test. In our sample you should see the output:

Advertisement

Hello Brighthub!

If you are interested in seeing what is happening during the compilation you can add the “-v” argument like so:

Advertisement

g++ -v -o test test.cpp

With the above command you will see a lot of text scroll by telling you what exactly is going on.

Advertisement

Final Thoughts

Being able to compile C++ code with g++ enabled me to get through C++ class. Without having to spend more money than a student should have to on a program. The g++ tool is an outstanding way to compile C and C++ applications on a Linux machine.

This post is part of the series: Linux configuring and programming tools

Linux can be a bit overwhelming as a new user. Fortunately there are plenty of tools to make this easier. In this Bright Hub Linux syntax commands series you will be introduced to various tools that will aid in the configuration and programming process.

Advertisement
  1. Linux Configuration and Programming tools
  2. Linux Command Line: g++
  3. Linux Command Line: which
  4. Linux Command Line: diff
Keep Exploring

More from Tech

Filed under
Linux Computing
More topics
Linux commands
Advertisement