How to compile and run C, C ++ programs on Linux

This simple tutorial shows you how to compile and run a C / C ++ program on a GNU / Linux operating system. If you are a student or a new Linux user coming from the Microsoft platform, you may be wondering how to run a C or C ++ program on a Linux distribution. Compiling and running code on the Linux platform is a bit different from Windows. Let’s get started

Setting up the development environment

As you may already know, you need to install the tools and compilers needed to run your code, right? Yes! To install all development tools on a Linux box, refer to the following guide:

  • How to install development tools on Linux

Development tools include the GNU GCC C / C ++ compiler, make, debugger, man pages, and all the necessary applications needed to compile and build new software and packages.

There is also a script named “manji” that will help set up a complete environment on Ubuntu based systems.

  • Manji-A simple script to set up a development environment on Ubuntu

After installing the required development tools, validate them using one of the following commands:

$ whereis gcc
$ which gcc
$ gcc -v

These commands show the installation path and gcc compiler version.

Compile and run C, C ++ programs on Linux

First, let’s see how to compile and run a simple program written in C.

Compile and execute a C program

Create code / programs with your favorite CLI / GUI editor.

Create a C program using Nano Editor.

$ nano ostechnix.c

Note: Must use extensions .c C program or .cpp For C ++ programs.

Copy and paste the following code.

int main()
   printf("Welcome To OSTechNix!");
   return 0;

A simple C program

Push Ctrl + O And Ctrl + X Save the file and exit.

To compile the program, do the following:

$ gcc ostechnix.c -o ostechnix


$ cc ostechnix.c -o ostechnix

If there are syntax or semantic errors in the code / program, they will be displayed. To go further, you must first fix them. If there are no errors, the compiler will successfully generate an executable named: ostechnix In the current working directory.

Finally, run the program using the command.

$ ./ostechnix

You will see output similar to the following:

Welcome To OSTechNix!

Compile and run C programs on Linux

To compile multiple source files (for example, source1 and source2) into an executable, run:

$ gcc source1.c source2.c -o executable

To allow warnings, debug the symbol in the output.

$ gcc source.c -Wall -Og -o executable

To compile source code into assembler instructions:

$ gcc -S source.c

To compile source code without linking:

$ gcc -c source.c

The above command creates an executable called source.o.

If your program contains mathematical functions:

$ gcc source.c -o executable -lm

See the man page for more information.

$ man gcc

Compiling and running C ++ programs

Create a C ++ program with any editor and save with extension .cpp.

This is a simple C ++ program.

$ cat ostechnix.cpp

int main()
  std::cout << "Welcome To OSTechNix!" << std::endl;
  return 0;

To compile this C ++ program on Linux, simply do the following:

$ g++ ostechnix.cpp -o ostechnix

If there were no errors, you can use the command to run this C ++ program on Linux.

$ ./ostechnix

Here is a sample output:

Welcome To OSTechNix!

Alternatively, you can use the above to compile the above C ++ program. "create" Commands such as:

$ make ostechnix

Did you notice? The above command did not compile the program using the .cpp extension. You do not need to compile C ++ programs with extensions. create command.

Execute using the command:

$ ./ostechnix
Welcome To OSTechNix!

Compiling and running C ++ programs on Linux

See the man page for more information.

$ man g++

Hope it helps.