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 :
– 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.
– 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!