MakefilesMakefilesMakefilesMakefilesTutorial adapted from Tutorial adapted from http://www.eng.hawaii.edu/Tutor/Make/http://www.eng.hawaii.edu/Tutor/Make/d d and and http://www.cs.bgu.ac.il/~sadetsky/openu/http://www.cs.bgu.ac.il/~sadetsky/openu/myMakeTutorial.txtmyMakeTutorial.txtCompiling a single programprogramCompiling multiple related filesrelated filesgcc green.c blue.cORORgcc –o program1 green.c blue.cMore efficient compilationMore efficient compilationMore efficient compilationMore efficient compilationgcc –c green c => create green ogcc c green.c => create green.ogcc –c blue.c => create blue.o bl li k d t gcc green.o blue.o => link and create a.out• The “c” flag compiles and creates gpobject files while ignoring functions and variables defined externally.ySplitting your C programSplitting your C program• When you separate your C program into yp y pgmany files, keep these points in mind: – When you use a global variable, •Define it in one source fileDefine it in one source file– int global_variable;• declare it in the header file– extern int global_variable;ld h h d fl h•include the header file in the appropriate C source files– Write all function prototypes in header files and include the header files in the appropriate and include the header files in the appropriate C source files.– Exactly one of the files must have a main() functionfunction.Dependency graphs and MakefilesMakefilesTarget: Dependencies<tab> How to compileMakefileMakefileall: project1project1: data.o main.o io.o<tab> gcc data.o main.o io.o –o project1MakefileMakefileMakefileMakefile•Create a file called Makefile•Create a file called Makefile.• At the prompt, type “make”.Make cleanMake cleanMake cleanMake cleanclean:clean:<tab>rm –f *.oAt the prompt:make cleanTo create more than one To create more than one executableexecutableexecutableexecutable1 Include the following line in the 1. Include the following line in the Makefileall: <exe1> <exe2> all: <exe1> <exe2> ….2. Write the rest of the Makefile.3. At the prompt, type “make”.To make more than oneTo make more than oneTo make more than one….To make more than one….•If you don’t want to re-create all •If you don t want to recreate all the executables every timeaSpecify only the ones that should a.Specify only the ones that should be with “all”.ORORb. Don’t use “all”. Instead, type:make <exe1> <exe2> ….Macros in MakefilesMacros in MakefilesMacros in MakefilesMacros in MakefilesCC = gccgDEBUG = -gLFLAGS = -Wall $DEBUG -oCFLAGS W ll $DEBUGCFLAGS = -Wall –c $DEBUGmain: green.o blue.o$(CC) $LFLAGS green o blue o$(CC) $LFLAGS green.o blue.ogreen.o: green.c common.h$(CC) $CFLAGS green.c$(CC) $CFLAGS green.cblue.o: blue.c common.h$(CC) $CFLAGS blue.cReferencesReferencesReferencesReferences• http://www.mtsu.edu/~csdept/FacilitiesAndResources/make.htm#What%20make• http://www.eng.hawaii.edu/Tutor/Make/• http://www.cs.bgu.ac.il/~sadetsky/openu/myMakeTutorial.txtDoubly linkedDoubly linkedlistlistDoubly linkedDoubly
View Full Document