The Makefile and Compiration Tutorial
Compilation
The first thing to remember about compiling, as with all things in computers, is Do not Panic! This will become second nature all too soon. The second thing to know about compiling is that when you write a program, you generally have nothing more THAN A TEXT FILE which is no difference to the computer tour... You've got to compile your program into an executable code which the computer can understand (There are such things as interpreted languages -. languages where your code is compiled as it is executed, but they do not count here since you do not compile them yourself .)
THIRD Thing to Know About Compiling Is That There received Some Distinct Steps in Compiling Some Code To an Executable. They Are:
Compiling the code to ask asmbly cot
This step is usually done transparently as most compilers perform it and then invoke the assembler themselves, so you do not really have to worry about it. It can be useful later on if you're trying to perform optimizations and you want to see how your compiler actually implements your code, if you can get your compiler to dump the assembly code that it generates. Other than that, just be aware that it happens and do not bother about it for a while. Assembling the Assembly code to object code
As you should know by now, a computer executes binary codes in its CPU. In order to run your program, your program will need to be translated into these binary codes. This is what this stage is about. The generated assembly (normally never actually present on your system but just passed from the compiler to the assembler) is translated into binary code. Some compilers will translate We're only the assembly code directly into an executable file, and others will leave it as what is known as object code. concerned with those that create object code. Object code is executable code which is not properly organized to run. It may be missing functions that it calls, it may be missing global variables, things like that. Generally, in C and C , object code is stored in files with the extension .o. Thus if you compiled your code into object code but did not link it, you would go from a program called hello_world.c to hello_world.o. Linking the object code together into an executableIf You Have SE veral object files (though you technically only need one), you can perform what is known as linking. This is the process of resolving dependencies between the object files, resolving dependencies of the object files on external libraries, and resolving addresses of global variables and Such. in Simple Terms, Linking Is The Process of Taking Code Which IS in Executable Form And Turn It Into Code Which CAN Actually Be Run by your computer on your operation system.
Compivering and linking
Example: Suppose the source code for an executable binary file named project.exe is stored in a file named project.cpp The program #includes a library's header file named lib.h and the library's implementation file is named lib.cpp Please download.. source_lab04_project.cpp and rename it to project.cpp. Then, do the same for source_lab04_lib.h and source_lab04_lib.cpp (renaming them to lib.h and lib.cpp, respectively) .The directive #include "lib.h" causes the compiler (actually, the preprocessor) to insert the declaration of the prototype for function doSomething (int x) into project.cpp. However, the definition of doSomething (int x) is in lib.cpp, not in project.cpp.
TRANSLATING A Program Like Project.cpp To Produce A Binary Executable Program Involves Three Phases:
Compiling, in which C code is translated into assembly; Assembling, in which assembly is translated into machine language; and Linking, in which calls to functions outside the main file are linked to their definitions All implementation files being used must be compiled as well. As The Main Program. The resulting Object Files Must the Be Linked Together To Produce a binary executable.
STEP 1:
Separately Compile the Program File
Name.cpp to produce an Object File Named
Name.o in Some Systems. This May Require Using A Special Compiler Switch, AS in The GNU C Command:
G -c Project.cpp
Project.cpp → Project.o
STEP 2:
Separately Compile Each Library Implementation File In The Same Manner; for example:
G -c lib.cpp
LIB.CPP → lib.o
STEP 3:
Link The Object Files Together Into a Binary Executable Program - for Example, IN GNU C with the command:
G Project.o Lib.O -O Project.exe
Project.o → Project.exe lib.o →
In Some Systems, Thase Steps Are Carried The Appropriate Menu Command - for Example,
Build Project.exe on the
Build Menu in Microsoft's Visual C and The
Make OR
Run) Command on The
Project Menu In Metrowerks' CodeWarrior C .
If a program involves several different libraries, it can be very difficult to keep track of which files are out of date, which need to be recompiled, and so on. Single commands like the preceding that do this automatically are very usefull in this regard. WITH GNU C , UNIX's make Utility CAN be used to execute a file named makefile what contains the commands for the compirations and linking.
UNIX's Make Utility
Makefiles
make is a system designed to create programs from large source code trees and to maximize the efficiency of doing so. To that effect, make uses a file in each directory called a Makefile. This file contains instructions for make on how to build your program and .
THERE THREE IMPORTANT PARTS TO A Makefile: The Target, The Dependencies, And The Instructions. Just So That You Know What this Will Look Like, It's of The Form: Target: Dependencies Instructions
Using the make utility requires a programmer to create a special file named Makefile, from which the make program reads information. A Makefile consists of pairs of lines (each pair governs the updating of one file).
Upon What Files Does
Project.exe defend?
→
Project.o and
Lib.o
(This means
Project.exe Won't compile without
PROJECT.O
and
Lib.o)
Upon What Files Does
Project.o dependend?
→
Project.cpp and
Lib.h
Upon What Files Does
LIB.O Depend?
→
LIB.cpp and
Lib.h
A
Makefile Has One Pair of Lines for Each File To Be "Made", SO Our Examplemakefile Will Have Three Line-PAIRS (ONE for
Project.exe, One for
Project.o and one for
Lib.o). Create a
Makefile now by Typing
Emacs makefile & at the unix propt.
The first line of each line-pair in a Makefile has the form: TargetFile: DependencyFile1 DependencyFile2 ... DependencyFilen where TargetFile is the file that needs to be updated, and each Dependencyi is a file upon which TargetFile depends.
..................
To illustrate: The first line-pair in Our makefile APPEARS AS FOLLOWS:
Project.exe: Project.o Lib.o
G Project.o Lib.O -O Project.exe
Note That The First Line Specifier The Dependencies of
Project.exe, and the second line is the unix command to make
PROJECT.EXE.
Of Course, Project.o Won't Exist Time We Compile, SO We Should Specify A Line-Pair for It, TOO:
Project.o: project.cpp lib.h
G -c Project.cpp
We shop .o:
Lib.o: lib.cpp lib.h
G -c lib.cpp
The Makefile Thus Appears As Follows:
Project.exe: Project.o Lib.o
G Project.o Lib.O -O Project.exe
Project.o: project.cpp lib.h
G -c Project.cpp
Lib.o: lib.cpp lib.h
G -c lib.cpp
Now, WHEN a User Types
Make
The Program Reads The
Makefile, And:
SEES That Project.exe Depends Upon Project.o, And
Checks Project.o, Which Depends on Project.cpp and lib.h; Determines WHETER Project.o is out of date; if SO, IT Executes the Command To make Project.o: g -c project.cpp; SEES THAT Project. EXE Also Depends Upon Lib.o, And
Checks Lib.o, Which Depends on lib.cpp and lib.h; determines WHETHER LIB.O is out of date; if so, it executes the command to make lib.o: g -c lib.cpp; SEES THAT EVERYTHING ON Which project.exe depends is now Up to date, and so executes the command to make provject.exe: g project.o lib.o -o project.exedetails about make.
While a Makefile Usually Consists of Pairs of Lines, There Can IN Fact Be Any Number of Commands After The Line Specifying The Dependencies. EXAMPLE: We Could Write, Project.exe: Project.o Lib.o
G Project.o Lib.O -O Project.exe
RM Project.o
RM LIB.O
This Would Automatically Remove The Object Files After Project.exe Is Made. The make Utility Also Allows a User To Specify What Is To Be worth: uname% makeli
will operate using lib.o as its primary TargetFile instead of project.exe. A TargetFile need not have any dependencies. This, combined with (1) and (2) allows make to be used for all kinds of non-compilation activities. Example : Suppose Our makefile Contains The Following Lines: Clean:
RM -F project.exe * .o * ~ * #
And the user type uname% make clean
What happens? (This is a fool-proof way to clean up a messy directory.) Make is coordinated with emacs. When An Emacs User Types The Command: M-X Compile
Emacs Responds with compile command: make -k
IF a Makefile IS in the Directory Containing The File on Which You are Working, THEN Pressing The Return Key Will Execute make use makefile.
Summary
The make utility eliminates the complexity of separate compilation by determining what files are out of date and re-making them Learning to use it effectively can save a great deal of time, especially on projects that have several files.Observation.:
There Can Be Only One File Named
Makefile in a Directory.
Conclusion:
Since a
Makefile Coordinates the Translation of One Project, Each Project Should Be Stored In Its Own Dedicated Directory, with a seat
Makefile to Coordinate Its Translation. Doing So Allows You To Remove The Object Files, Binary Executables, etc.
CD to the Directory and Type
Make.
An Added Benefit Is That All The Files for One Project Are Confined With Directory, Making It Easier To Port The Project To a DiFerent Machine. (Just Copy The Directory To the New Machine,
CD to the Directory, and Type
Make).
Hand In: This lab handout with the answers filled in attached to a listing of your final Makefile (use the enscript command from your Programming Style Sheet to print it out: enscript -E -G -2rj -M Letter -PECT2_PS