The “make” program
Managing Large Programs
• large programs can be very large: thousands or even millions of lines in hundreds of files
Copyright By PowCoder代写 加微信 powcoder
• a C program can have many .c and .h files
• whenever we change one of the files (.c or .h) we have to recompile all of the unchanged ones as well
• this is very time consuming
• we have C files:
labels.c readml.c printml.c util.c
• we make a change to util.c and then recompile by:
gcc labels.c readml.c printml.c util.c -o labels
• we are recompiling ALL the C files when only one has been changed
• We can break down each stage of the compilation by using separate programs or by providing flags to the compiler
• Preprocessing
• Syntax checking
• Translation to assembly
• Compose object files
• Link symbols between object files
• Produce binary executable
Example using .o files
• we initially compile the C files to .o: gcc -c labels.c readml.c printml.c util.c
• we make a change to util.c and then recompile by:
gcc -c utils.c
gcc labels.o readml.o printml.o util.o -o labels
• we are recompiling the file that has been changed and then relinking with the other .o files
• BUTifwehavealargenumberoffilesand make many changes, how do we remember which files we’ve changed and recompile them?
Problem: what happens if we change a .c file and forget to recompile it?
Problem: what happens if we change a .h file and forget to recompile some of the files that #include it?
Answer: your program fails!
or gives the wrong answer!
The real answer: use the make program to recompile and relink your files
Why does the program fail?
• if changes are made to data structures but code that refers to it is not recompiled, the references may now go to the wrong place
char b; } X;
char b; } X;
• a function that referred to X.b would not get the correct value if it wasn’t recompiled after the change
• the make program will automate the process of recompiling and recompile all the files that have been changed
• make interprets a set of rules and will run the C compiler and the linker as necessary to make your program
• the rules are normally stored in a file called Makefile or makefile
Make rules
• in general, make rules have:
• a target: the name of the file you want to make
• one or more dependencies: files the target depends on
• an action: a shell command that creates the target
Example rule
• to make mystring.o from mystring.c we might use the rule:
mystring.o: mystring.c mystring.h gcc -c mystring.c
dependencies
Example rule
• if mystring.c or mystring.h have changed since mystring.o was created then the action is performed to recreate mystring.o
• how does the make program know that a file has been changed?
# first version of makefile for labels program labels: labels.o util.o
gcc labels.o util.o -o labels
labels.o: labels.c ml.h gcc -c labels.c
util.o: util.c ml.h gcc -c util.c
• To remake the labels program just type: make
Default rules
• these rules are very repetitive: every action is the same except for the file name
• make provides a set of default rules for common situations
• these defaults can be overidden and rules for new file types can be entered
Default rules
• for example, there is a default rule that specifies how to make a .o file from a .c file:
filename.o : filename.c
gcc -c filename.c
# second version of makefile for labels program # (using default rules)
labels: labels.o util.o
gcc labels.o util.o -o labels labels.o: ml.h
util.o: ml.h
• make knows the default rule for labels.o and util.o
• still have to specify a rule for the case where ml.h changes
Combining rules
• you can combine rules when the targets have common dependencies and actions:
labels: labels.o util.o
gcc labels.o util.o -o labels
labels.o util.o: ml.h
Rules without dependencies • itisoftenusefultocreaterulesthatdon’thave
dependencies and always activate
• forexample,aruletocleanupthefilespace: labels: labels.o util.o
gcc labels.o util.o -o labels labels.o util.o: ml.h
Make variables
• sometimesyouneedtorefertoalistoffilesat several places in the make file
• ifyouretypethelistineachplaceyoucan introduce errors if you leave out a file in one or more places
• makehasavariableormacrofacilitythatletsyou type the list in one place and refer to it using the variable name
Make variables • assignmentshavetheform:
variable_name = any character sequence
• thevalueofthevariableissubstitutedwith:
$(variable_name)
# makefile for labels program
OBJECTS = labels.o util.o
labels: $(OBJECTS)
gcc $(OBJECTS) -o labels
$(OBJECTS):ml.h
rm $(OBJECTS)
Predefined variables
• makehasanumberofpredefinedvariables:
CC the default C compiler CFLAGS flags passed to the compiler
• youcanchangethevalueofthesevariables:
CFLAGS = -DDEBUG -W -Wall -pedantic -ansi
this would cause the DEBUG symbol to be defined
• alargesystemmightconsistofseveralprograms that share a number of functions
• librariesgiveyoutheabilitytostoretheobject code versions of the functions in one place and have them linked into your program
• therearemanylibrariesoffunctionsprovidedby the system
• thestandardClibraryhasthestandardI/O functions, system functions, sort function, etc
• thestandardlibraryisautomaticallysearched when your program is linked
• youcanusefunctionsfromotherlibrariesbyusing the -l flag when linking, eg
gcc -lm myprog.o -o myprog
• thiswillsearchthestandardmathsfunctionlibrary for functions such as sin, cos etc
• theCcompilerwillsearchforthelibraryin standard directories: /lib, /usr/lib
Making your own libraries
• youmakeyourownlibrariesusingthe“ar” command on the .o files:
ar c mylib.a readit.o util.o
• thelibrarywillbecalledmylib.aandwillcontain your .o files
• youcanusethelibrarywithacommandlike:
gcc myprog.o mylib.a -o myprog
• Cprogramsveryquicklygettoasizewherethey require several files
• largeprogramscanhavehundredsof.cand.h files that depend on each other
• themaketoolhelpsyoumaintainanup-to-date version of your program
• librariescanalsohelpmaintainlargesystems
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com