CS 201 Makefile Debzani Deb 1 Remember this
CS 201 Makefile Debzani Deb 1
Remember this? 2
What is a Makefile? • A Makefile is a collection of instructions that is used to compile your program. – "make" is a program that looks for a file called "makefile" or "Makefile“ and executes the instructions in it. • Makefiles are very helpful when dealing with projects (having many source files). – Once you modify some source files in your project, and type the command "make“, your program will be recompiled using as few compilation commands as possible. 3
A Typical Makefile # Makefile for hello. c # Debzani Deb, CS 201 # hello: hello. o gcc -o hello. o: hello. c gcc -c hello. c The filename must be Makefile hello. c Source file hello. o Object file hello Executable file • Whenever, “make” command is issued in the command prompt, this file is executed. 4
A Typical Makefile # Makefile for hello. c # Debzani Deb, CS 201 # hello: hello. o gcc -o hello. o: hello. c gcc -c hello. c COMMENTS: always starts with # 5
A Typical Makefile # Makefile for hello. c # Debzani Deb, CS 201 Target’s Prerequisite # hello: hello. o DEPENDENCY line gcc -o hello. o RULE line: must starts with a tab hello. o: hello. c gcc -c hello. c • hello depends on hello. o , if hello. o changes, then hello must be rebuild. And in order to rebuild hello , the Rule below dependency line must be executed. 6
A Typical Makefile # Makefile for hello. c # Debzani Deb, CS 201 # hello: hello. o gcc -o hello. o: hello. c gcc -c hello. c Time stamp on the file hello is compared to the time stamp of file hello. o. DEPENDENCY line If the target’s time stamp is earlier than prerequisite’s time stamp, the rule below this dependency line is executed. 7
Can you see the similarities? # Makefile for hello. c # Debzani Deb, CS 201 # hello: hello. o gcc -o hello. o: hello. c gcc -c hello. c 8
Makefile (Summary) • • • Build a dependency line for every target. Add a rule line to rebuild the target. Start rule line with a tab. Add comments to the top of the file. Save these lines in an ASCII file called Makefiles are very powerful. Pretty much anything that needs to be compiled (postscript, java, Fortran), can utilize makefiles. 9
- Slides: 9