1 / 12

Makefile

Makefile. Ansuman Banerjee Arijit Bishnu Debapriyo Majumdar Data and File Structures Lab M.Tech . Computer Science 1 st Year, Semester I Indian Statistical Institute Kolkata August 26, 2014. Compilation stages. Compiler stage: C file  lower level assembly language code

Download Presentation

Makefile

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Makefile AnsumanBanerjee ArijitBishnu Debapriyo Majumdar Data and File Structures Lab M.Tech. Computer Science 1st Year, Semester I Indian Statistical Institute Kolkata August 26, 2014

  2. Compilation stages • Compiler stage: C file lower level assembly language code • Assembler stage: Assembly language code  Object code (machine directly understands) [.o files] • Linker stage: Object codes  Linked to produce an executable file May also link with some built in functions

  3. The Codebase • Download: http://www.isical.ac.in/~pdslab/ProgramsForMakefile.tar.gz • Trivial compilation: $ gccmain.cadd.cmean.c -o prog • Create object files, and compile $ gcc-c add.c -o add.o $ gcc –c mean.c -o mean.o $ gcc-c main.c-o main.o $ gccmain.omean.oadd.o -o prog

  4. Dependencies • $ gcc -c add.c -o add.o • $ gcc –c mean.c -o mean.o • $ gcc -c main.c -o main.o • $ gccmain.omean.oadd.o -o prog

  5. The mean function Arithmetic mean Geometric mean Which object and executable files should change? • #include "mean.h" • int mean(intn1, intn2) { • return (n1+n2)/2; • } • #include "mean.h" • #include <math.h> • int mean(intn1, intn2) { • return sqrt(n1*n2); • }

  6. Recompiling # Recompiling only mean.o and the prog $ gcc –c mean.c -o mean.o $ gccmain.omean.oadd.o -o prog • A Makefile • Define dependencies and compilation instructions beforehand • When parts of a large codebase is changed, the make command recompiles selectively and builds the program

  7. A Simple Makefile # comment target: dependency1 dependency2 ... <tab> build instruction / command The format prog: main.omean.oadd.o gcc -Wall main.omean.oadd.o -o prog main.o: main.cadd.hmean.h gcc -c -Wall main.c -o main.o mean.o: mean.cmean.h gcc -c -Wall mean.c -o mean.o add.o: add.cadd.h gcc -c -Wall add.c -o add.o Makefile

  8. A Simple Makefile $ make $ make prog Command prog: main.omean.oadd.o gcc -Wall main.omean.oadd.o -o prog -lm main.o: main.cadd.hmean.h gcc -c -Wall main.c -o main.o mean.o: mean.cmean.h gcc -c -Wall mean.c -o mean.o add.o: add.cadd.h gcc -c -Wall add.c -o add.o Specifying the Target Makefile

  9. Defining more targets default: prog all: prog prog: main.omean.oadd.o gcc -Wall main.omean.oadd.o -o prog -lm main.o: main.cadd.hmean.h gcc -c -Wall main.c -o main.o mean.o: mean.cmean.h gcc -c -Wall mean.c -o mean.o add.o: add.cadd.h gcc -c -Wall add.c -o add.o Makefile If there were more executable files, prog1 prog2 … or the object files

  10. Variables default: prog all: prog prog: main.omean.oadd.o gcc -Wall main.omean.oadd.o -o prog -lm main.o: main.cadd.hmean.h gcc -c -Wall main.c -o main.o mean.o: mean.cmean.h gcc -c -Wall mean.c -o mean.o add.o: add.cadd.h gcc -c -Wall add.c -o add.o Makefile Objects Command Flags

  11. Variables CC=gcc CFLAGS=-c –Wall LDFLAGS=-lm OBJECTS=main.oadd.omean.o default: prog all: prog prog: $(OBJECTS) $(CC) $(OBJECTS) -o prog $(LDFLAGS) main.o: main.cadd.hmean.h $(CC) $(CFLAGS) main.c-o main.o mean.o: mean.cmean.h $(CC) $(CFLAGS) mean.c-o mean.o add.o: add.cadd.h $(CC) $(CFLAGS) add.c-o add.o clean: rm -f $(OBJECTS) Makefile

  12. Experiments for yourself • Make clean and then run make • Change the file add.h and run make • Change the file main.c and run make

More Related