Home FAQ How To Compile And Run C, C++ Programs In Linux
Run C, C++ Programs In Linux

How To Compile And Run C, C++ Programs In Linux

By sk
2027 Views

This brief tutorial explains how to compile and run C/C++ programs in GNU/Linux operating system. If you're a student or a new Linux user coming from Microsoft platform, then you might be wondering how to run the C or C++ programs in a Linux distribution. Because, compiling and running code in Linux platforms is little bit different than Windows. Let us get started, shall we?

Setup Development Environment

As you may already know, we need to install the necessary tools and compilers to run the code, right? Yes! Refer the following guide to install all development tools in your Linux box.

The development tools includes all necessary applications, such as GNU GCC C/C++ compilers, make, debuggers, man pages and others which are needed to compile and build new software, packages etc.

Also, there is a script named 'manji' that helps you to setup a complete environment in Ubuntu-based systems.

After installing the necessary development tools, verify them using any one of the following commands:

$ whereis gcc
$ which gcc
$ gcc -v

These commands will display the installation path and version of gcc compiler.

Compile And Run C, C++ Programs In Linux

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

Compile And Run C Programs

Write your code/program in your favorite CLI/GUI editor.

I am going to write my C program using nano editor.

$ nano ostechnix.c

Note: You need to use extension .c for C programs or .cpp for C++ programs.

Copy/paste the following code:

#include <stdio.h>
int main()
{
   printf("Welcome To OSTechNix!");
   return 0;
}

Simple C program

Press Ctrl+O and Ctrl+X to save and quit the file.

To compile the program, run:

$ gcc ostechnix.c -o ostechnix

Or,

$ cc ostechnix.c -o ostechnix

If there are any syntax or semantic errors in your code/program, they will be displayed. You need to fix them first to proceed further. If there is no error then the compiler will successfully generate an executable file named ostechnix in the current working directory.

Finally, execute the program using command:

$ ./ostechnix

You will see an output like below:

Welcome To OSTechNix!

Compile and run C program in Linux

To compile multiple source files (Eg. source1 and source2) into executable, run:

$ gcc source1.c source2.c -o executable

To allow warnings, debug symbols in the output:

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

To compile the source code into Assembler instructions:

$ gcc -S source.c

To compile the source code without linking:

$ gcc -c source.c

The above command will create a executable called source.o.

If your program contains math functions:

$ gcc source.c -o executable -lm

For more details, refer the man pages.

$ man gcc

Compile And Run C++ Programs

Write your C++ program in any editor of your choice and save it with extension .cpp.

Here is a simple C++ program.

$ cat ostechnix.cpp
#include <iostream>

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

To compile this C++ program in Linux, simply run:

$ g++ ostechnix.cpp -o ostechnix

If there were no errors, you can run this C++ program under Linux using command:

$ ./ostechnix

Sample output would be:

Welcome To OSTechNix!

Alternatively, we can compile the above C++ program using "make" command like below.

$ make ostechnix

Did you notice? I didn't use .cpp extension in the above command to compile the program. It is not necessary to use the extension to compile C++ programs using make command.

And run it using command:

$ ./ostechnix
Welcome To OSTechNix!

Compile And Run C++ Programs in Linux

For more details, refer man pages.

$ man g++

Hope this helps.

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More