C / C++ Programming in Linux – For starters

I have had a lot of requests on snippets of how to do C/C++ programming in Linux.
Thanks to KS sir ( yeah Khushil Saini sir) for giving us intro a proper way. Without him, I would have probably used DevCpp and lived on Windows. I will try to keep it short and simple unlike his notes 🙂

Linux used is Ubuntu (any version will do). Language considered: C/C++(In case you are new to Linux, you will have to get accustomed to some terms before you try this. See the Operating Systems/Linux category)

– Run the terminal(the command prompt for Linux).

– Before starting off, better make a new directory to keep codes in a place.
command: mkdir codes

– Move into codes :
cd codes

– Now make a  source code, famous “helloworld” using :
gedit helloworld.c [.c for C and .cpp for C++]
This will open up a text editor. Write the code in it and save.

– Next job is to compile it and make an executable :
gcc helloworld.c / g++ helloworld.cpp

– Now observe in directory that an executable is formed : a.out [It is similar to a .exe in Windows]

– To run the program :

– It will print Hello World.

Note that:
– The clrscr(), doesn’t work.
– The getch() is not required to hold for output as the program will run on terminal only and the output will be available even after program has exited/ completed.
– Printing a “\n” at the end of program is advisable for clarity on terminal (A personal choice, not a strict rule).
– Whenever you compile a newer code, a.out will be replaced with the latest executable. To avoid that you can use ‘-o’ option with gcc/g++ to rename the output.
e.g — >  gcc helloworld.c -o hello
Make sure there is no file with same name as hello. Otherwise it will be overwritten.
Hope it works out great for all those who are moving to Linux. Have fun!


One thought on “C / C++ Programming in Linux – For starters

  1. Great work, Sir.
    This post is surely going to help people wanting to compile C/C++ programs in accordance with the latest gcc/g++ compilers. At first, I used to use the same method mentioned but found it a bit tedious especially when you need to debug your program. Compiling and executing the program requires to type in 2 commands. So, I would recommend making a small bash script which compiles and executes and takes the name of the output file and input file as parameters.
    However, I still find using IDEs like Devcpp provided they have the latest compilers installed in them. 🙂
    Informative article. Looking forward to more in the future.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s