In this case, we can compile the 3 source files main.c, hello.c, and world.c separately, but we need all the object files to exist before we can link them together.
(Otherwise, linking main.o will complain about missing references to the functions hello() and world(), while hello.o and world.o both will complain about a missing main() function.)
Anyhow, if we create our Makefile correctly, we don't have to bother about this:
# The executable 'helloworld' depends on all 3 object fileshelloworld: main.o hello.o world.o
cc -o helloworld main.o hello.o world.o# Line starts with TAB!# Build main.o (only requires main.c to exist)main.o: main.c
cc -c main.c# Line starts with TAB!# Build hello.o (only requires hello.c to exist)hello.o: hello.c
cc -c hello.c# Line starts with TAB!# Build world.o (only requires world.c to exist)world.o: world.c
cc -c world.c# Line starts with TAB!# Remove object files, executables (UNIX/Windows), Emacs backup files, and core files
clean:
rm -rf *.o helloworld helloworld.exe *~ *.core core# Line starts with TAB!
This Makefile has 5 targets, helloworld, main.o, hello.o, world.o, clean.
To build the application, type
make helloworld
or (as helloworld is the first target)
make
As we see, the target helloworld has 3 prerequisites, the object files main.o, hello.o, world.o.
If the prerequisites already exist, make just links them together.
But if for example the file hello.o does not exist, make will look for the target hello.o, build it (which in this case means compiling hello.c) before it links the object files together.
To build only the object files, type
make main.o; make hello.o; make world.o
or just
make main.o hello.o world.o
To remove object files and the application executable, Emacs backup files etc., type
make clean
As we can see, the target clean has no prerequisites.
No particular file needs to exist to cleanup undesired files, which makes sense.
You can name this target whatever you want, but the common praxis is to use the name clean.
The difference between the target clean and the other targets is that no file exists (nor will be created) with the name clean.
This kind of target is called a phony target.
(As you may have noticed, the target hello in our previous example is also a phony target - there will never exist a file named hello.)
To be sure that a phony target works as expected, it should be marked as .PHONY : target.
Normally a phony target will work anyway, but if a file named clean exists, we may get unexpected results:
Note: make keeps track on source code timestamps and object file timestamps.
If a source code file is newer than an object file (which means it has been modified since last time it was compiled), or the object file does not exist (which means the source code was never compiled), make recompiles the source code, otherwise not.
Exercise:
Make a cleanup.
Build the application. Check the make output.
Build the application a second time. Compare with the previous make output.
Modify one of the source code files.
Build the application a third time. Compare with the previous two make outputs.