CSE 20211 - Fundamentals of Computing I - Univ. of Notre Dame Instructor: Topics
CSE 20211 - Fundamentals of Computing I - Univ. of Notre Dame Instructor: Topics
of Notre Dame
Topics
• Makefiles – An additional primer
Makefiles
• Purpose
o Compile an executable or executables that consist of multiple source files
together
• Method
o Using UNIX commands, build the executable or even build + install the
executable
o Think of a makefile as any sequence of UNIX commands
o In our case, sequence of gcc commands
• Structure
o Collection of rules
o What is a rule?
The target or name of the rule is what we want to produce. Each dependency is a
file that goes into the sequence of commands to produce the target. All
commands are one tab in from the left.
o Example:
main.o : main.cc
gcc –c main.c
o Example 2:
main.o : main.cc
gcc –c main.c
Lab1 : main.o
gcc –o Lab1 main.o
o What did that do?
make main.o
make Lab1
This will create Lab1 and if necessary, also create main.o in order to create Lab1.
clean:
rm *.o
install: Lab1
cp Lab1 ~/.
more README
Copy the Lab1 executable to the main source directory and then show the
README file using more. Keep in mind that a makefile can execute any
command we can execute at the command prompt. In other words, we could
compile a Java file, set the time/date, change file permissions, list directories, etc.
• Example
# Output program
EXE = Lab1
$(EXE) : $(OBJS)
$(COMPILER) –o $(EXE) $(OBJS)
main.o : main.cc
$(COMPILE) main.c
clean:
rm *.o