[How to] Program on Linux using gcc

Hey! It took me a while to figure out that Linux is in fact the best OS for a non-gamer like me. And whenever I DO login Windows, even by mistake, I am greeted by a slew of viruses and a painfully slow speed. 😮

So 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.4, 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 hey presto! 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.

And, 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, so why not learn it now? 😉


He is super awesome guy who is fascinated by gadgets, internet and everything 'tech'y. He blogs at The BlankBored

  • Anjana singh

    gcc is not  installed in my os how could i install it

  • Anonymous

    sudo apt-get install gcc

© 2011 Suhas Tech. All rights reserved.
Proudly powered by Wordpress.