The Open Source of Hovering Ideas

Programming

C/C++ Programming on Linux using GCC


Hey Guys !! I’m back again with some Technical Stuff once again.

Well Just few days back, I was in search for a place where i could get proper techniques of C/C++ Programming under Linux Platforms using GCC. Actually, I’ve mostly used Windows in my Life and as a matter of fact, I’ve used either Turbo C++ or Dev C++ under Windows Platforms. I even didn’t required anything else so far. But since my MAD Professor insisted us to practice C++ in Linux, my search for E-books, Documents and Softwares beings,  for understanding C/C++ in Linux using GCC/G++ compiler, begins.

It took me a while to figure out that Linux is in fact the best Operating System or OS, for typical Non-Gamers unlike me. 😛 And whenever I DO login Windows, even by mistake, I am greeted by a slew of viruses and a painfully slow speed. Anyways, I figured I should do even my C programming on Linux. It was there in my coursework from college you know!

There are two ways in which you can program in C in Linux:

1) Use an IDE (Integrated Development Environment) like Netbeans or Codeblocks.

OR

2) Use the inbuilt GCC compiler, like I did.

GCC basically stands for GNU C Compiler. Its not actually an IDE, but it kinda works when you get used to it. Plus, its easier to setup. Many people (including me) were in a total state of confusion at first, but hey, turns out its pretty simple!

Let me illustrate the steps you need to take to compile and build a program on a Linux machine.

1) Type out your code in a text file (on gedit) and give it a “.c” extension. It actually works. Atleast in Ubuntu 10.10 Maverick, your pre-processor code will have a different colour than the rest, and so on, like a normal IDE.

2) Go to Applications -> Accessories -> Terminal. Use the command “cd” to navigate to the directory where your “.c” file is stored. Eg: If your file is stored in “Documents” under the root folder, the type “cd Documents” in the terminal.

3) If your filename is say, “code.c”, then type “gcc code.c -o code”. This compiles and builds your required file.

4) Type “./code” to run your program. And Its all done !!

And of course, if u need to use C++, just replace gcc” by “g++”. And save the file as “.cpp”. The rest is all basically the same. Even sometimes, It can happen that GCC may not be installed in your OS. Not a problem. It will automatically prompt you to install it in a couple of simple steps, which require you just pressing “y/n”.

At the end of the day, Linux programming may not give you a very distinct advantage, but hey, its different! Its a skill that you’ll need to learn eventually maybe and since I got to know how to get with it easily, so why not learn it now? 😉