Compile C/C++ Programs on Linux

Introduction: Compile C/C++ Programs on Linux

About: I have a very bad habit of laughing at serious moments. :v xD

This article will show you how to compile a C/C++ program on Linux using the GNU gcc/g++ compiler.

Hopefully this article will serve as a guide and tutorial to compiling GNU C/C++ programs on Linux.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Install Build-essesntial Package

Open up a terminal on Ubuntu Linux and install the build-essential package by typing the following command in the terminal

sudo apt-get install build-essential

These are the important library files for compiling C/C++ programs on linux.

Step 2: Create a Seperate Folder to Hold Your Programs

Use the below command to make a directory on your desktop.

sudo mkdir -p CPP/HW/

By using the above command we are making a folder named 'CPP' & a sub-folder named 'HW' inside it.

Step 3: Change Your Working Path to Your Created Directory

Change your working directory in the Terminal to the folder which you just created by using the below command.

cd CPP/HW/

Step 4: Creating the Program File With 'nano'

We will use 'nano' an inbuilt simple text editor to create & write programs & their files. Use the command below to make your program file.

sudo nano helloworld.c

Step 5: Write Your Program Code and Save It

type your program code into the nano editor. For the sake of this tutorial I'll be making a simple helloworld program.

Program :



int main()


printf("\nHello World,\nWelcome to my first C program in Linux");



Now save your program by pressing Ctrl+X on your keyboard at the same time and ' press Y ' and press Enter

Step 6: Compiling Your Program

Type the below command to compile your program

gcc -Wall -W -Werror helloworld.c -o helloworld

The first line will invoke the GNU C compiler to compile the file helloworld.c and output (-o) it to an executable called helloworld

The options -Wall -W and -Werror instruct the compiler to check for warnings.

If you want to compile a C++ program then use the below command

g++ -Wall -W -Werror helloworld.cpp -o helloworld

Step 7: Executing Your Program

Type the below command to execute your program.

sudo ./helloworld

we typed ./helloworld since our compile version in the previous step is helloworld. If its xyz then we will type ./xyz

After typing this your program output will be shown in the Terminal.
Thank you.

As Shown on

Be the First to Share


    • Backyard Contest

      Backyard Contest
    • Silly Hats Speed Challenge

      Silly Hats Speed Challenge
    • Arduino Contest 2020

      Arduino Contest 2020

    2 Discussions


    5 years ago on Introduction

    Thank you very much for sharing this with the Instructables community, I appreciate it!

    Vamsi Srikanth
    Vamsi Srikanth

    Reply 5 years ago on Introduction

    You're welcome peppy.
    If there are any groups in instructables, then please send me the link so that this can be shared.

    And also you can ask me for tutorials also.