Developing C/C++ programs under ubuntu

I already covered how to write c++ programs in linux. But in that article, i didn’t mentioned about how to install compilers and stuff. So this post will cover how to install compilers in ubuntu and to write, compile and run program.

Installing C/C++ compilers

Enter this into terminal (Note: you’ll be asked for root password in ubuntu)

sudo aptitude install build-essential

If you follow the steps mentioned in terminal it will download and install the respective packages. After installation, you’re all set for compiling c++ programs.

Writing C program

In ubuntu, you’ve to open text editor. i.e. gedit. you can do that from terminal or can launch gedit manually.

Let’s run it from terminal this time.

sudo gedit helloworld.cpp

Now you’ve to write hello world program, you can write as simple like this. Now to compile the program, type this into terminal (Make sure you’re in same directory as that of file. if not, “cd” it and go the directory where file exist).

cc -c helloworld.cpp

If everything went well then this will create an object file. Now to run this program you’ve to create an binary file. Run this command in terminal.

cc -o helloworld helloworld.cpp

Now you’ve the executable which you can run by typing following command.

./helloworld

Now you’ve the helloworld program running.