How To Compile And Run a C/C++ Code In Linux

See all GNU/Linux related FAQ
I am a new Linux user and student who used to write C or C++ programs on MS-Windows. Now, I am using Ubuntu Linux. How can I compile a C or C++ program on Linux operating systems using bash Terminal application?

To compile a C or C++ program on any Linux distro such as Ubuntu, Red Hat, Fedora, Debian and other Linux distro you need to install:
Advertisement

  1. GNU C and C++ compiler collection
  2. Development tools
  3. Development libraries
  4. IDE or text editor to write programs
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux terminal
Category Software Development
Prerequisites GNU C/C++ compiler
OS compatibility AlmaLinux Alpine Amazon Linux Arch CentOS Debian Fedora Linux Mint openSUSE Pop!_OS RHEL Rocky Stream SUSE Ubuntu
Est. reading time 3 minutes
See all UNIX/Linux Programming/Development related articles

Step #1: Install C/C++ compiler and related tools

If you are using Fedora, Red Hat, CentOS, or Scientific Linux, use the following yum command to install GNU c/c++ compiler:
# yum groupinstall 'Development Tools'
If you are using Debian or Ubuntu Linux, type the following apt-get command/apt command to install GNU c/c++ compiler:
$ sudo apt-get update
$ sudo apt-get install build-essential manpages-dev

Step #2: Verify installation

Type the following whereis command and which command to display the version number and location of the compiler in Linux:
$ whereis gcc
$ which gcc
$ gcc --version

Fig. 01: GNU C/C++ compilers on Linux

Fig. 01: GNU C/C++ compilers on Linux

How to Compile and Run C/C++ program on Linux

Create a file called demo.c using a text editor such as vi, emacs or joe. For example:

#include<stdio.h>
/* demo.c:  My first C program on a Linux */
int main(void)
{
 printf("Hello! This is a test prgoram.\n");
 return 0;
}

How do I compile the program on Linux?

Use any one of the following syntax to compile the program called demo.c:
$ cc program-source-code.c -o executable-file-name
## OR ##
$ gcc program-source-code.c -o executable-file-name
## OR, assuming that executable-file-name.c exists ##
$ make executable-file-name

In this example, compile demo.c, enter:
$ cc demo.c -o demo
OR
## assuming 'demo.c' exists in the current directory ##
$ make demo

If there is no error in your code or C program then the compiler will successfully create an executable file called demo in the current directory, otherwise you need fix the code. To verify this, type:
$ ls -l demo*

How do I run or execute the program called demo on Linux?

Simply type the the program name:
$ ./demo
OR
$ /path/to/demo
Samples session:

Animated gif 01: Compile and run C and C++ program demo

Animated gif 01: Compile and run C and C++ program demo

Compiling and running a simple C++ program

WARNING! The file extension for C++ is case-sensitive. You can use either ‘.C’ (capital C) or ‘.cpp’.

Create a program called demo2.C or demo2.cpp as follows:

#include "iostream"
// demo2.C - Sample C++ program 
int main(void) 
{
    std::cout << "Hello! This is a C++ program.\n";
    return 0;
}

To compile this program, enter:
$ g++ demo2.C -o demo2
OR
$ g++ demo2.cpp -o demo2
You use the following syntax too:
$ make demo2
To run this program, type:
$ ./demo2

How To Compile And Run a C or C++ Code In Linux

Click to enlarge

How do I generate symbolic information for gdb and warning messages?

The syntax is as follows C compiler:
$ cc -g -Wall input.c -o executable
The syntax is as follows C++ compiler:
$ g++ -g -Wall input.C -o executable

How do I generate optimized code on a Linux machine?

The syntax is as follows C compiler:
$ cc -O input.c -o executable
The syntax is as follows C++ compiler:
$ g++ -O -Wall input.C -o executable

How do I compile a C program that uses math functions?

The syntax is as follows when need pass the -lm option with gcc to link with the math libraries:
$ cc myth1.c -o executable -lm

How do I compile a C++ program that uses Xlib graphics functions?

The syntax is as follows when need pass the -lX11 option with gcc to link with the Xlib libraries:
$ g++ fireworks.C -o executable -lX11

How do I compile a program with multiple source files?

The syntax is as follows if the source code is in several files (such as light.c, sky.c, fireworks.c):
$ cc light.c sky.c fireworks.c -o executable
C++ syntax is as follows if the source code is in several files:
$ g++ ac.C bc.C file3.C -o my-program-name
See the ‘gcc(1)’ and other related Linux manual pages for more information using the man command or help command:
$ man gcc
$ man g++
$ man cpp
$ man make

This entry is 7 of 13 in the Linux GNU/GCC Compilers Tutorial series. Keep reading the rest of the series:
  1. Ubuntu Linux Install GNU GCC Compiler and Development Environment
  2. Debian Linux Install GNU GCC Compiler and Development Environment
  3. CentOS / RHEL 7: Install GCC (C and C++ Compiler) and Development Tools
  4. Download and Install C, C++ Compiler on Red Hat Enterprise Linux 5 (RHEL)
  5. Mac OS X: Install GCC Compiler with Xcode
  6. Where is My Linux GNU C or GCC Compilers Are Installed?
  7. HowTo: Compile And Run a C/C++ Code In Linux
  8. RHEL / CentOS Linux Install Core Development Tools Automake, Gcc (C/C++), Perl, Python & Debuggers
  9. HowTo Compiling C Program And Creating Executable File Under a Linux / UNIX / *BSD
  10. How To Install ncurses Library on a Linux
  11. Linux Find Out What Compilers Are Installed or Available On The System
  12. Linux Find Out GNU gcc Compiler Version Used To Compile Running Kernel
  13. Howto see output of C program in Linux or UNIX

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

39 comments… add one
  • ADHI P Nov 7, 2016 @ 2:54

    I am Learning C++ in Linux and I am Getting an ERROR for the command or To compile this program as follow:
    # g++ demo2.c -o demo2

  • Tommy Nov 9, 2016 @ 2:28

    when i want to display the output. it prints the printf content, but when i use scanner, i dont get an option to input data. Help??

  • RAJDEO JHA Sep 22, 2020 @ 13:26

    there are data base in binary system is file,
    the c processing to work a application a file,
    there are application to placement value a
    prework value application to place
    sub+ calculus= Inter/value*work/pkace( equation 1)
    =|state \vertical
    =|statev3/value*work/place
    =|state v3*unit
    even number=operator/operation (equation2)

  • Antonina Janek Mar 15, 2021 @ 19:37

    I was looking to compiling, linking and building C/C++ applications on Linux. Page gave me enough instuctions to do other stuff too. Thank you Mr. Vivek for all your effort.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.