Compile C/C++ Programs On Linux
Compile C/C++ Programs On Linux

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.

Step 1: Install build-essesntial package

Compile C/C++ Programs On Linux

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

Compile C/C++ Programs On Linux

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

Compile C/C++ Programs On Linux

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'

Compile C/C++ Programs On Linux

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

Compile C/C++ Programs On Linux
Compile C/C++ Programs On Linux

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

Program :

#include

#include

int main()

{

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

return(0);

}

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

Compile C/C++ Programs On Linux

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

Compile C/C++ Programs On Linux
Compile C/C++ Programs On Linux

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

http://www.mightygeekstuff.com/2015/01/compile-c-programs-on-linux/

 
 

Tag cloud

make build easy simple arduino making homemade solar laser printed portable cheap mini building custom cardboard wooden create super lego turn paracord chocolate your paper light intel