LETS "MAKE" IT....

0 views
Skip to first unread message

Harinderjit Singh

unread,
Aug 19, 2009, 8:00:06 AM8/19/09
to JUIT Linux User Group


suppose you are working on a project .the code of project deals with
many files of code
i.e. there are many headers files (.h) and .cpp files involved.thus
there would be some dependencies
among these files.Compiling can be a issue in such a case.

here is a simple solution to avoid compilation anomalies.Solution is
to make a "makefile" in the folder of project
and use a simple command "make" to compile your project.see how simple
is that just one command and you are done.
but you should be able to make a makefile for that.

The make command allows you to manage large programs or groups of
programs. As you begin to write larger programs, you will notice that
re-compiling larger programs takes much longer than re-compiling short
programs. Moreover, you notice that you usually only work on a small
section of the program (such as a single function that you are
debugging), and much of the rest of the program remains unchanged.

The make program aids you in developing your large programs by keeping
track of which portions of the entire program have been changed,
compiling only those parts of the program which have changed since the
last compile.


MAKING A MAKEFILE:

This section describes the make program in more detail by describing
the file it uses, called makefile or Makefile.
first you should be aware of the dependencies in your project.i rekon
"learn to make dependency graph first",thats very helpful .

target : source file(s)
command (must be preceded by a tab)

example-:

OBJECT=main.o
CC=g++

main: $(OBJECT)
$(CC) $(OBJECT) -o main

main.o: main.cpp
$(CC) -c main.cpp


clean:
rm -rf *o main



1. here in above code OBJECT and CC are variables .OBJECT has been
assigned value main.o which is an expected object file of a small
project where CC has the name Compiler.

2. main:
is the entry point of the makefile.instead of (main:) ,i could use
(all:) also.here main is the target and value of OBJECT(main.o) is
source file.
now to solve for this dependency,it will look for main.o in the
makefile.main.0 has the dependency on main.cpp and since there nomore
dependencies the
command $(CC) -c main.cpp will be executed and main.o will be
formed.

3. since make is recurcive in nature,it will jump back to main: and
execute the command $(CC) $(OBJECT) -o main

4. now the output file named main has been created by make in the
project folder.run it and see.

RUNNING MAKE

harinderjit@harinderjit-laptop:/media/HARRY/c++/demo-header$ make
g++ -c main.cpp
g++ main.o -o main
harinderjit@harinderjit-laptop:/media/HARRY/c++/demo-header$

this is how it looks ,when it runs fine.
don't forget to consult GOOGLE for details.

this was small effort to introduce make to people who didn't
know,people who know more ,should come up with suggestions and
correct me wherever i am wrong.

THANKS AND REGARDS

note: this tutorial doesnot discuss everything about make.please
GOOGLE for more information

Chirag

unread,
Sep 13, 2009, 7:38:49 AM9/13/09
to JUIT Linux User Group
nicely written tutorial, great effort. :)

Shashank Singh

unread,
Sep 13, 2009, 8:20:12 AM9/13/09
to juit-linux...@googlegroups.com
Good tutorial 
btw if you are working on very large projects like OS , Desktop Environments , PDF reader what not ... then generally sometihng more automatic than make is desired hence some good coders came up with
[1] Auto tools (Cross Platform , characterized by command ./configure ;) ,used by gnome DE )
[2] Cmake (Cross Platform , cmake .  [with a dot], used by KDE )
[3] Qmake build system (Cross Platform )
[4] Nmake build system (Windows only platform)
[5] Ant build system (Specially for Java)

[1],[2],[3] essentially configure and generate a Makefile , so you don't have to write make file , but if you still love old-school geek'ism ..you can always hand code Makefile as explained in the excellent tutorial .. do give it a try 


IMHO cmake is one of the easiest build systems out there .
--
Regards:
Shashank Singh
Blog:http://techfreaks4u.com/blog
Marble contributor : http://edu.kde.org/marble/
KDE-in Volunteer : http://www.kde.in/

Chirag

unread,
Sep 13, 2009, 11:29:45 AM9/13/09
to JUIT Linux User Group
@shanky: the old school geek-ism is always there dude... lol

On Sep 13, 5:20 pm, Shashank Singh <shashank.perso...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages