How to Compile a C++ file in Linux

In programming languages, writing code is the most basic skill in creating programs. There exist numerous languages have existed/evolved over time. Out of these languages, C++ is no doubt extremely commonly used in the world of programming. Unlike other languages, such as HTML and CSS as C++ must be compiled manually. The compilation takes the human-written code and turns it into a form that can be read and executed by the system.

In this article, knowledge will be provided on how you can compile a C++ file on your Linux operating system.

How to Compile a C++ File Linux?

There exist various methods through which a C++ file can be compiled and executed on the Linux operating system. As an example, we use the following C++ file in this article:

Now, let’s head over to the methods to compile the C++ file on Linux.

Method 1: Use the G++ Compiler

how to handle linux files and direc...

The “G++” compiler is the most commonly used compiler for both C and C++ files due to its simplicity. Mostly this compiler comes built into the Linux operating system, but if it is not present on your system, it can simply be installed using the command below:

$ sudo apt install g++

Once the compiler is installed on your system, you can use this method, simply execute the command that is shown below:

$ g++ sample.cpp

The command above uses the “G++” keyword to compile the file whose name is mentioned at the end of the command, which is the “sample.cpp” file. This creates a compiled file with the name “a.out”. This compiled file can then be executed as shown below:

$ ./a.out

In the case that you want to compile the file with a different name than “a.out”, you can use the “-o” option as demonstrated below:

$ g++ -o compiled_file sample.cpp

Through this simple method, the C++ file can be easily compiled.

Method 2: Using the GCC Compiler

Much like the “G++” compiler that was demonstrated in the previous section, GCC is also another compiler, but this compiler is utilized for both C and C++ programs. If this compiler does not exist on your system by default, you can simply install it with the command shown below:

$ sudo apt install gcc

After its installation, you can utilize it by using the keyword “GCC” along with the name of the C++ file which in this case is the “sample.cpp” file. Furthermore, this compiler executes the “-lstdc++” option. This option signifies that the compiler will search the C++ library for its compilation.

The first command below compiles the program, and the “GCC” creates an executable file named “./a.out”. The second command executes the “./a.out” file:

$ gcc sample.cpp -lstdc++
$ ./a.out

Here you go! You have learned to compile a C++ file using the “GCC” and “G++” compilers.

These were the two most common methods that are used to compile a C++ file in Linux.


In Linux, the “GCC” and “G++” compilers can be used to compile a C++ program. The “G++” compiler takes the name of the file to be compiled, whereas the “GCC” compiler requires an additional option, “lstdc++” with the file name. This post has given a detailed explanation of each of these methods to compile a C++ program on Linux.