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

Sunday, 2 May 2010

Intro to emacs as C\C++ source code editor



Hi guys.....
Well whatever I'm writing in this blog can be found in help,info,man pages or any book as its the starting details of emacs.
I actually presented this presentation as a talk in OSScamp Pantnagar,May 2010.
Its just for review or Introduction.
(Intro to Programming in Linux can be found at http://gnu-linux34915.blogspot.com/2010/05/starting-programming-in-gnulinux.html)
Well, An editor is the program that you use to edit source code. Lots of different editors are available for Linux, but the most popular and full-featured editor is probably GNU Emacs.
Emacs is the free, customizable, extensible, platform independent editor(means its available for different platforms).
Emacs is much more than an editor. It is an incredibly powerful program, so much so that at CodeSourcery, it is affectionately known as the One True Program, or just the OTP for short. You can read and send email from within Emacs, and you can customize and extend Emacs in ways far too numerous. You can even browse the Web from within Emacs!

Emacs is a very powerful tool written by richard M. stallman and has more than 1000 of useful commands. Some of those commands which I think are good for start using emacs and helped me to start programming C\C++ in emacs are:

General Commands
C-x-f : To open a file
C-x-s: To save a file
C-x-c: To close Emacs(All buffers)
C-x 2: To split buffer horizontally
C-x 3: To split buffer vertically
C-x 1: To close all the buffers except current
C-x 0: To close the current buffer
M-x : To enter a command
M-x compile: To compile
M-x gdb: GNU Debugger
Meta Key
Under Linux: the Esc key
Under Windows: the Alt key
Under MacOs it should be the Command key.

Here one thing should be noted 'C' is used for Ctrl key and c-x-f is different from c-x 0.
C-x-f means to press all the keys Ctrl, x and f simultaneously and C-x 0 means first press Ctrl an x together then leave both the keys , the command interpretor will wait for the next instruction or command input , press 0 and the command will work.

Note: I'm making blog as explainable and easy to understand as possible without technical words , if there is any contradiction or mistake please feel free to comment and correct me I will surely take it under consideration.

Now why is emacs good for programming when many other powerful editors like vi and vim or easy GUI editor like gedit or kwrite (or notepad++ in M$) are available.
Well here are some features of emacs that make it handy for C\C++ programming:

Advantages of emacs for programming:
Editing , Compiling , Debugging all in one editor.
Multiple buffers
Auto Indentation (Automatic Formatting)
Syntax Highlighting
Different Modes
Command + GUI both interface

To make it work out more simpler let me give brief info about emacs with the help of some screenshots.
Well emacs is not by default available in GNU-Linux distros.
You can install it by typing #apt-get install emacs22 or just use Synaptic package manager, for windows download the exe file.
I will be particularly talking about the use of emacs in linux(Ubuntu).
to start emacs its in Applications->Accessories or type emacs in the terminal (with file specified optional).
To open a file type Ctrl+x+f command.



Now go to the file which you want to edit , here I would i like to add a thing if you will open C or C++ file it will identify the mode and adds an extra C\C++ menu in emacs.













As in above pics you can see Different color coding are used for keywords, identifiers and all i.e., Syntax highlighting.
Now let us compile the C or C++ code.
We will be using gcc\g++ to compile the codes.
First press M-x (M is meta character , Esc in ubuntu Linux) , you will see that the command interpreter will wait for the next instruction , input compile and press enter.
It will show make -k i.e., the default command to compile in Linux. Clear it and type gcc -c(arguments optional) and press enter.
Your code will successfully compile inside emacs.
You can see how in the next screenshots.










You not only compile you can even run GNU debugger (gdb) inside emacs using M-x gdb command.
At last close emacs using C-x-c command.
There is a lot to learn. Keep learning keep blogging and do comment.
Here are some videos too (No audio).






I hope this Intro will help you guys start using emacs.

Aduait Pokhriyal
Student
B.Tech 3rd Year

My first Post

Hello world!!!!