How to write C or C++ programs on Ubuntu Linux

If you are a newbie in the linux world you are the facing the question – how to compile c program in linux or how to compile c++ program in linux. So in this post I’m going to explain the steps using which you can run your c c++ programs on linux.

There are two ways to write and run C and C++ programs on Ubuntu.

  1. Using command line
  2. Using IDE (app)

Using command line

To run a C or C++ program on linux, there are three phases write code, compile code and run it. First you have to write your code using a text editor eg. gedit (it is just like Notepad++). Then after writing the code you have to manually compile the code using particular commands, the compiler creates an executable file ( just like .exe in windows) for our code and then we run that executable file

For compile C programs you need gcc c compiler linux tool which is installed by default on Ubuntu. For running C++ programs you need g++ tool which is not installed by default on Ubuntu. You have to install it by running command

sudo apt-get install g++

Running a C program

NOTE : #include<conio.h> will not work on Ubuntu.

Writing C program

Create a text file named test.c

Now open a terminal and execute the below command

gcc test.c -o test

Here gcc is the tool using which you will compile your C program. test.c is the file name of your code (‘.c’ is the extention of a C program). -o argument is passed to tell the compiler that while making the executable file of my program test.c, name it as test. You can also use another name other that test like myExecutedFile or something. Now run your code by executing the command below.

./test

In linux ./ is used to run an executable file. In our case executable file is test

Running C program

Running a C++ program

Writing C++ program

Create a text file named test.cc (you can also use test.cpp)

Now open a terminal and execute the below command

g++ test.cc -o test

Here g++ is the tool using which you will compile your C++ program. test.cc is the file name of your code (‘.cc’ and .cpp are the extentions of a C++ program). -o argument is passed to tell the compiler that while making the executable file of my program test.cc, name it as test. Now run your code by executing the command below.

./test
Running C++ program

Using IDE

Using IDE, running a C or C++ program is very easy and productive. Here we are going to use Geany application. So first install geany by using the command below.

sudo apt-get install geany
Geany menu bar

Now create a new file test.c in geany.
Compile using Build Menu > Compile
Build using Build Menu > Build
Execute using Build Menu > Execute

Geany IDE C programming

You can also compile, build and execute directly using buttons.

You can do the same for C++ programs also.


Comments

Leave a Reply

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