Monday, 14 June 2010

Programming in GNU/Linux environment Part 2

Intro to makefile:

In my previous blog I gave the Introduction for starting Programming in Linux environment.In this blog I'm planning for further writing about gcc compiler and options available and at last an Introduction to make file.
Whenever we compile any C program using gcc by default the output is a.out file.To specify the output we use -o option with gcc:
gcc -o main main.c
Here main will be the output file assuming main.c is the C source code file available.
Now if we have two different C/C++ program files dependent on each other for executing we can not create binary file or executable directly we have to first create object files and then combine them.For example we have main.c and reciprocal.cpp files and main function is in .c file which calls another function reciprocal in .cpp file to calculate reciprocal of a entered number.Now first we will create object files of both C and C++ programs using -c option.
gcc -c main.c
g++ -c reciprocal.cpp
These two commands will create two files named main.o and reciprocal.o respectively.Now to create object files we can use command:
g++ -o reciprocal main.o reciprocal.o
Sometimes because of optimisation by compiler or due to any other reasons our program may not work as we have expected.We use debugger to solve this problem.To use GNU debugger we use -g option with gcc compiler to add extra information for gdb(GNU debugger):
g++ -g -o reciprocal main.o reciprocal.o
One more thing we are using g++(C++) compiler to combine object files as one of the file(reciprocal.cpp) is of C++ if both files were of C we can use gcc to create binary.
Now here consider the case you have more than 2 or 3 source code files now to write gcc command again and again is time consuming and chances for error increases , here comes the makefile useful.Makefile is a collection of all gcc lines over and over again.Its very useful especially if you are making a lot of changes to a code and it has several libraries.Here change in one file means compiling all the code and running all commands again.But with makefile single make command will compile all the code in single go.
Consider the above reciprocal program.The makefile will look like:
reciprocal: main.o reciprocal.o
gcc -g -o reciprocal main.o reciprocal.o
main.o: main.c
gcc -g -c main.c
reciprocal.o: reciprocal.c
gcc -g -c reciprocal.c
Here there are two types of lines dependent and executable.All the gcc lines having a preceded tab are executables that will compile program and produce output and the lines just above tell the dependencies of output.For example reciprocal is dependent on main.o and reciprocal.o so before executing gcc -g -o reciprocal main.o reciprocal.o main.o and reciprocal.o will be produced and it continues till dependencies are satisfied.All the above content will be in file named makefile and running make command will compile the program if makefile in current directory.
If you want to delete/remove all the *.o files and reciprocal binary just created You can add clean entry in makefile using rm command.
clean:
rm -f *.o reciprocal
Hope it will help.Do comment and keep learning.

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).

video


video

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

Aduait Pokhriyal
Student
B.Tech 3rd Year

My first Post

Hello world!!!!