How to Compile and Run C/C++ programs in Ubuntu

How to Compile and Run C/C++ programs in Ubuntu

Compiling C/C++ programs are most easier in Linux using terminal using commands. There is no separate software required to download on your system. Only you have to install a free Linux package called gcc, which is responsible to compile and run C/C++ program.

If you have gcc package already installed, that’s good. otherwise you can follow the following commands to install gcc on your Linux system.

$  sudo apt-get update
$  sudo apt-get install gcc

RUNNING C++ ON UBUNTU COMMAND LINE TERMINAL - LINUXHACKR

Just only 3 steps to follow and you get it!

1.Writing to program in editor

Then you have to write your C/C++ program in editor. You can choose any editor for example nano, gedit, vim etc your choice. But, for simplicity and small programs I prefer gedit.

Write the code using following commands. If filename is hello.c then we have to type

$  gedit hello.c

then gedit application automatic opened and then you can write your c/c++ program. for example,

press ctrl + s to save and close the gedit application.

RUNNING C++ ON UBUNTU COMMAND LINE TERMINAL - LINUXHACKR

2.Compiling the program hello.c

Then you have to compile your program which you have written. make sure you have written your code correctly, otherwise gcc gives you error!

$  gcc hello -o hello.c

we write hello here to create a executable file which help us yo run our program.

RUNNING C++ ON UBUNTU COMMAND LINE TERMINAL - LINUXHACKR

3.Running the program

Now we have to run our program.

$  ./hello

RUNNING C++ ON UBUNTU COMMAND LINE TERMINAL - LINUXHACKR

that’s it.

Note this thing only for creating and compiling the small programs, but if you want to write a large program or you are working on a project which have large number of files inside the one single project. Then you don’t have to use gedit or any simple editors to write your program and gcc for compile your program.

In order to write and compile a large program or project of c/c++ or any type of programming language, you need a separate software which you can download from the Internet or Ubuntu snap store.

There are lots of Editors and IDEs for writing and compiling the code but I am telling you the best Compilers for Linux here. I highly recommend the Eclipse IDE for C/C++. It has great features and it can compile PHP, JS, Java etc languages also.

I recommend to read this How to install Eclipse in easy way for C/C++, java. java EE, PHP

So, Good luck! do happy coding.

I hope this article will helpful for you. <3

You May Also Like

About the Author: Linux Hackr

I love to writing Linux blogs. I'll be very happy if you say something in comment box, because it's not just comment it is my motivation. Thank you !

17 Comments

Leave a Reply