



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




(A modified version of a tutorial available at http://mrbook.org/tutorials/make/)
Overview: You have likely used the *nix utility make before. For large software projects, dividing the code into multiple source files is the only reasonable approach. When you recompile the project, you need only recompile the source file that you changed and those that depend upon it. Manually recompiling is a non-starter โ determining which source files to recompile becomes overwhelming as the size of your project grows.
The purpose of the make utility is to determine automatically which pieces of a large program need to be recompiled, and issue the commands to recompile them.
This lab is intended to help you better understand how make and Makefiles work.
Executing the Lab: On the course Web site, you will find the gzipped tarball lab3-files.tgz โ download and unpack the file into the directory of your choice. This tarball contains the source code necessary for the lab and contains your electronic answer sheet.
As you read through this document, execute the commands as directed. Whenever you encounter a command that is also preceded by โQ#โ, answer the corresponding question in your answer sheet.
The Makefile : The make utility reads a special file known as a makefile. The makefile (or Makefile) describes the files involved in the project and the dependencies among them. Each group of lines in a makefile has the following form.
targets: dependencies
In this context,
If any of the files in a dependency list is modified, make will recompile and/or relink the target.
Important: a
Task 1: Presented below are the contents of the very simple makefile named Makefile provided in the tarball for this lab.
hello: hello.cpp factorial.cpp main.cpp functions.h g++ -Wall -Wno-deprecated -g -o hello hello.cpp factorial.cpp main.cpp
In this context:
Q1. make
Q2. make
Q3. touch functions.h; make
The problem with this approach is that if any file in the dependency list changes, all of the source files must be completely recompiled. For example, you may have changed only hello.cpp, but because of the compile command used in the makefile, each of hello.cpp, factorial.cpp, and main.cpp must be recompiled (and then linked) โ clearly not ideal.
Task 2: Start by removing the existing executable hello and then create a new (initially empty) makefile named Makefile2:
rm hello; touch Makefile
One way to avoid having all files be recompiled each time is to make use of object files. To create an object file for the hello.cpp source file, you could issue the command g++ -Wall -Wno-deprecated -g -c hello.cpp (but do not do so here). The result would be a file hello.o. This file is not executable because no linking has taken place โ only compilation.
Create a rule in your Makefile2 for creating hello.o similar to the following:
hello.o: hello.cpp functions.h g++ -Wall -Wno-deprecated -g -c hello.cpp
Notice that the dependencies for hello.o are hello.cpp and functions.h. The hello.cpp source file as a dependency should be obvious. The function.h is also a dependency because it is included in the file hello.cpp.
Now create similar rules for creating main.o and factorial.o. (What dependencies do these two need?)
By default, make will look for a makefile named makefile and Makefile in that order. To override and use a differently-named makefile, use the -f flag. In the current context, we want to use Makefile2 as our makefile.
Q4. make -f Makefile
CC = g++ # the compiler to use LD = $(CC) # the command used to link objects
INCDIR = -I../ # additional directories to look for include files
CCFLAGS = -Wall -Wno-deprecated -g -c $(INCDIR) # compiler options LDFLAGS = -Wall -Wno-deprecated -g # linker options
From the above example, you should notice:
Add the above macro definitions to the beginning of Makefile3. Then change each of the target rules to use the macros. For example, the target to create main.o should now look like:
main.o: main.cpp functions.h $(CC) $(CCFLAGS) main.cpp
Note that your command to create the final executable should use the LD and LDFLAGS โ after all, you are simply linking object files in this step rather than compiling.
Q12. make -f Makefile3 clean; make -f Makefile
Further, you can create macros to represent lists of files. For example, you can create a macro to represent all the object files as follows.
OBJS = hello.o main.o factorial.o
Add this macro to Makefile3 and then use the macro in all appropriate places.
Q13. make -f Makefile3 clean; make -f Makefile
Modify the CC macro to use gcc instead of g++.
Q14. make -f Makefile3 clean; make -f Makefile
Now change the CC macro back to use g++.
Task 5: Start by copying Makefile3 to Makefile4.
Our target rules now look like the following:
main.o: main.cpp functions.h $(CC) $(CCFLAGS) main.cpp
We were able to replace the command and string of arguments using macros. Can we do a similar thing for the files-to-be-compiled?
The make utility also provides the following special-purpose macros:
Change the target rule for main.o by replacing main.cpp in the command with $^.
Q15. make -f Makefile4 clean; make -f Makefile4 main.o
Change the rules for all .o targets to replace the .cpp file with $< in the commands. Also change the rule for the hello target to use $@ appropriately in the command.
Q16. make -f Makefile4 clean; make -f Makefile
Task 6 : Start by copying Makefile4 to Makefile5.
Look at the makefile. You should notice that the three rules for the .o targets look very similar. The only differences are the names of the .o and .cpp files (they all require functions.h).
The make utility provides the wildcard % to address just such situations. We can replace all three .o rules with a single rule similar to the following:
%.o: %.cpp functions.h $(CC) $(CCFLAGS) $<
The % character, when it appears in the target portion of a rule, acts as a wildcard, matching any non-empty portion of a filename. In the dependencies portion of the rule, % stands for the same portion of the filename the % had matched in the target portion of the rule. For example, if the make utility was trying to produce main.o, it would match %.o against main.o and then replace % in %.cpp with main to obtain the first dependency main.cpp.
Remove the three existing .o rules from Makefile5 and replace with a single wildcard rule as given above.
Q17. make -f Makefile5 clean; make -f Makefile
Task 7: Some additional notes about compiling using makefiles:
Q18. g++ -MM *.cpp