CC++C++17Introduction to C++Learn C++

How To Run A C++ Program In The Linux Terminal

Linux is a great operating system, and the C++ Language is the one of the most powerful programming languages. You can compile your C++ app code with the latest C++ version of most C++ build tools as well as a number of C/C++ IDEs with compilers in all sorts of different operating systems like Windows, iOS, Android as well as Linux. C++ is very popular among developers in all OS platforms. Using a fast and reliable C++ compiler for an operating system is very important for beginners and professionals since it helps C++ developers in remembering which language features exist, how to use them, and even detect errors when we get them wrong.

Linux might be one of the most popular platforms in the world, because many servers and even mobiles running Android are powered by the Linux operating system (Android is a highly customized version of Linux). Linux is also supported by the other operating systems in VMs or in simulations. Linux is free and open source, and this has contributed greatly to its success and popularity.

Why run a C++ program in Linux?

There are two types of programming languages: interpreted and non-Interpreted (compiled). All computers work with machine code, code that can be directly understood by computer’s process, that tells the computer what to do. This is the lowest level, native and fastest code, but it requires writing many lines for simple things and is hard to generalize for all kinds of machines. A compiler (C or C++ Compiler, etc.) is a computer program that converts one programming language i.e., C/C++ code written with text into executable machine code with a linker.

GNU C/C++ compilers are the one of most popular programming platforms for the Linux operating system, but there is a good selection of different programming languages for Linux too. For example, RAD Studio / Delphi is a very good non-interpreted native programming language for Linux that also has support for the Python language with GUI’s and graphics.

Despite being ‘high level’ – which means it’s easy for humans to understand – the C/C++ language is extremely efficient and when compiled it can produce programs which achieve processing speed which is very close to pure hand-written machine code instructions.

How to run a C++ program in Linux Terminal on Windows

Until recently the best way to run Linux apps on Windows was using the mingw system, However, with arrival of Windows 10 and Windows 11 there is now a thing called WSL – short for Windows Subsystem for Linux. WSL is a specialized, highly optimized, lightweight version of virtual machine which allows Windows to host many different types of Linux distributions. In the WSL distro users can run nearly any Linux program they wish, including GUI apps. We’ve written about WSL on the Embarcadero Blog and alsl talked about how you can develop and run full Linux GUI apps on WSL.

How To Run A C++ Program In The Linux Terminal an image of the WSL terminal running on a desktop PC

How to run C++ program in Linux Terminal with the GNU C/C Compiler?

GNU GCC is a powerful C/C++ compiler originally written as the standard compiler for the Unix, Linux and GNU operating systems. The GNU system was developed to be 100% free software (free from cost but also free from hidden restrictions on licenses and affiliations with businesses), and it is efficient with both 32-bit and 64-bit operations. This compiler can be used on Windows using WSL as well the MinGW or CygWin Linux simulators. Compiled executable files need a small dll to run independently on Windows. It is mostly compatible with CLANG standards. Please check https://gcc.gnu.org/ for more details about GNU.

Here is a screen shot of a Linux system showing the Sublime Text editor, and the Linux Terminal,

How To Run A C++ Program In The Linux Terminal the terminal window in action

Is there an example of how to run a C++ program in Linux terminal?

If you are using an actual Linux machine rather than a Windows machine, then you can use any text editor to create C++ files. Sublime Text Editor is a light editor for beginners. If you want only to edit in console Vim editor is good. Eclipse CDT is a modern C++ editor for Linux. Spyder is a good editor for Python and C++. There are other C++ editors which can be used in different Linux distributions. Some of these are, VS Code for Linux, and CodeBlocks for Linux. Let’s write our C++ code as below,

Here is an example of C++ code,

Let’s explain this small example. When you start coding in C or C++, first of all, you should include libraries that you use. Generally for beginners <iostream> library header is enough to do simple beginner apps with input and output operations in C language. We can include this library as below,

Second, you should add the main procedure (the main program, the main function) as below,

In C++, after every command you use you should put a semicolon – the “;” – to the end of your commands. In this example above we print a text with cout method of iostream, then we exit by return 0; which means our main function in C++ app successfully run and exit. If you want to add more code, you should write your programming code lines into this procedure, between { and } brackets before your application returns 0.

If you are a beginner “Hello World” examples are a good way to understand feature of that programming language. .

Here is a “Hello World” C example that can be run with C++ Builder ,

In this example above we define the main() function as an integer (int) function, that means we should return an integer value. Here, we print a text with printf() function and then we exit and return 0; which means our main C app successfully run and exit.

After these steps, in Linux terminal, go to folder of your C++ file and type gcc and your file name. Be sure that you are supervisor or allowed to save files to that folder. There will be a.out as an executable file.

For example, you can compile your app as below,

How to compile a C++ program in Linux with the gcc compiler ?

On Linux, GNU C/C++ comes as the default programming language already installed.

If you are using WSL on Windows, then you can use the following command in the WSL terminal to install GCC:

Generally, the gcc compiler is used as a C compiler and g++ compiler is used as a C++ compiler. If your c program file is myapp.c you can compile this with gcc command as below,

Be sure that you have permission to write to folder there. Now you can easily run your myapp.out as given below,

How to run a C++ program in Linux terminal?

Here is the full input and output process in Linux terminal

Note that, you can compile C++ and C files with g++.If you want to compile c language projects you can use gcc c compiler too but you can not compile C++ files gcc compiler.

If you are looking for C and C++ Compiler for Windows, iOS or Android,

How To Run A C++ Program In The Linux Terminal the C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version y

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Learn C++SyntaxTemplates

What Are The Logical Operation Metafunctions In Modern C++?

C++C++14C++17C++20Learn C++

What Are The Deprecated C++14 Features In C++17?

C++C++14C++17C++20Learn C++

What Are The C++14 Features Removed From C++17?

C++C++11C++14C++17C++20Learn C++Syntax

What is the conjunction (std::conjunction) metafunction in C++?