Thursday 20 May 2010

Starting programming in GNU/Linux environment (Introduction)

Its my personal experience that when it comes to IDE then the environment Linux or windows doesn't matter but if you are a programmer or starting learning basics(core) of any programming languages or want to build your concepts in any programming language then Linux environment is worth to give a try.
In my this blog I'm going to just tell the basics which are helpful for the students(people) starting Linux.
I'm going to reference terminal and simple editor for coding and compiling/interpreting codes here. There are lots of editors available in Linux which are by default installed with OS installation like gedit/kwrite or vi but I prefer emacs editor for C/C++ programming. You can read about emacs at http://gnu-linux34915.blogspot.com/2010/05/intro-to-emacs-as-cc-source-code-editor.html .
Now to compile program codes Linux have GNU Compiler collection. It includes gcc for C code compilation , g++ for C++ codes and gcj compiler for java code.
Lets first start with gcc. gcc is default available with Linux-OS but then too to check type the command whereis gcc in the terminal.
If you get output like shown below then you have gcc installed in your system but if output is just gcc: then you will have to install it using package manager. gcc uses standard library glibc for compiling programs you can check it too using command whereis glibc .


Just to show how to start programming lets use geditor right now. So we have gcc compiler , simple editor and standard library to compile. So now lets write our first program in C two print Hello World !!:


#include
int main(int argc,char** argv)
{
printf("Hello World !!\n");
return 0;
}


save it with any name having .c extension let us assume its filename.c then open terminal and write the command gcc filename.c
It will compile the source code and produce executable binary of the program and if there are any error it will report error messages with line numbers.
By default gcc produces a.out named binary.(Note in windows its filename.exe if the case is TC compiler)
You can execute this binary by ./a.out command.


This file can not be executed on windows due to different format. You can also specify the name of the output binary file while compilation by using -o argument. Just write gcc -o print filename.c where print is the name for binary file every thing else is same.


If you are writing a C++ code then g++ is used to compile the source code every thing else is same. Consider the code written in filename.cpp file.


#include//.h is not written according to latest standard
int main()
{
using namespace std;//can also use std::cout
cout<<"Hello World !!\n"; return 0;
}



Now in case of java source code you can either use java compiler javac or gcj compiler from GNU compiler collection but in my experience I find java compiler javac and java run time environment more comfortable. To use javac in linux its same as windows.
If you are learning C/C++ I would prefer you to use emcas editor .
In my next few blogs I'm have extended this topic and discuss about options available in gcc and Introduction to makefile.
Till then keep learning ... keep sharing and do comment if you like the post or how it can be improved.
Till then
take care
good byee

Aduait Pokhriyal

No comments:

Post a Comment