|
File: "hello.c" #include <stdio.h> int main( int argc, char* argv[] ) { printf( "Hello World !\n" ); } |
Source file: hello.c | | gcc -c hello.c | V Object file: hello.o | | gcc -o hello hello.o | V Executable: hello |
My Terminology: "dependent" file and "depend on" file
hello depends on hello.o ^ ^ | | "dependent" file "depend on" file |
Summary:
hello depends on hello.o Command to update hello when hello.o is changed: gcc -o hello hello.o hello.o depends on hello.c Command to update hello.o when hello.c is changed: gcc -c hello.c hello.c: is changed/updated by the programmer... |
|
Source file: hello.c | | gcc -c hello.c | V Object file: hello.o | | gcc -o hello hello.o | V Executable: hello |
hello: hello.o # Meaning: target hello depends of hello.o <TAB> gcc -o hello hello.o # Command to make the target hello.o: hello.c # Meaning: target hello.o depends of hello.c <TAB> gcc -c hello.c # Command to make the target |
|
Example:
Source file: hello.c <==== We update thsi file | | gcc -c hello.c | V Object file: hello.o | | gcc -o hello hello.o | V Executable: hello |
The Make utility will then:
|
How to run the program:
|
|
|
# Comment lines can appear anywhere Macros section (will be discussed later) Rules section |
|
We will discuss macros later in this webpage
|
|
targetName : depend-file1 depend-file2 .... <TAB> UNIX-command-1 <TAB> UNIX-command-2 ... <TAB> UNIX-command-N |
Meaning:
|
hello.o: hello.c # Meaning: target hello.o depends of hello.c <TAB> gcc -c hello.c # Command to keep the target up to date |
Meaning:
|
|
main: func5a.o func5b.o # main depends on func5a.o and func5b.o gcc -o main func5a.o func5b.o func5a.o: func5a.c # func5a.o depends on func5a.c gcc -c func5a.c func5b.o: func5b.c gcc -c func5b.c func5a.c: func5b.h touch func5a.c func5b.c: func5b.h touch func5b.c |
Note:
|
main: func5a.o func5b.o # main depends on func5a.o and func5b.o gcc -o main func5a.o func5b.o func5a.o: func5a.c func5b.h # func5a.o depends on func5a.c gcc -c func5a.c # and (indirectly) also on func5b.h func5b.o: func5b.c func5b.h gcc -c func5b.c |
How to run the program:
|
|
main: func5a.o func5b.o gcc -o main func5a.o func5b.o func5a.o: func5a.c func5b.h gcc -c func5a.c func5b.o: func5b.c func5b.h gcc -c func5b.c |
make targetName # Make the target targetName |
I.e.: we can make different targets by using an additional argument to the make command !
Example:
make func5a.o make func5b.o make main |
|
Example:
|
|
% = wildcard character - match 0 or more characters $< = first item in the list of dependent filenames $^ = all items in the list of dependent filenames $@ = current target name including the extension $* = current target name not including the extension |
%.o: %.c func5b.h gcc -c $< |
Meaning:
|
main: func5a.o func5b.o gcc -o main func5a.o func5b.o func5a.o: func5a.c func5b.h gcc -c func5a.c func5b.o: func5b.c func5b.h gcc -c func5b.c |
main: func5a.o func5b.o gcc -o main func5a.o func5b.o %.o: %.c func5b.h gcc -c $< |
Note: this Makefile is even better
main: func5a.o func5b.o gcc -o $@ $^ %.o: %.c func5b.h gcc -c $< |
How to run the program:
|
|
|
# Comment lines can appear anywhere Macros section (will be discussed later) Rules section |
|
We will now discuss macros...
(You have seen the rules section)
|
src-pattern = replacement-pattern |
Meaning:
|
Purpose:
|
|
CC=gcc # Compiler command CFLAGS=-c # Compiler options OFLAG=-o # Output flag main: func5a.o func5b.o $(CC) $(OFLAG) main func5a.o func5b.o %.o: %.c func5b.h $(CC) $(CFLAGS) $< |
How to run the program:
|
|