The most commonly used features of make can be broken down into a couple simple concepts, targets, dependencies and variables.
Targets are the things you want to build, but the command(s) beneath a target can be compiler commands or scripts. Generally each target refers to a module in your code, but you can make these as granular as you want to suit your project.
Dependencies are files or other targets in your project. The best example of this is for a C project where you're building a binary from a bunch of object files. Each object file will need to exist before you can build the binary, so make will traverse your targets until all of the dependencies have been completed, and then run the command for the overall target.
Variables aren't always necessary, but are pretty handy for handling things like compiler flags. The canonical examples are CC and CCFLAGs which will refer to the compiler your using i.e. gcc and the flags like -ansi -Wall -o2.
A couple more general tips and tricks:
- Commands must be proceeded by a [tab] character, or they won't be executed, this is just an old relic of make, I don't recall why this is.
- By convention, you may want to include an all target to specify the default which target should be the default. This is useful when you have a complex makefile and there's a particular target you always want to be the default.
- Your makefile should be called makefile or Makefile, but if you want to call it something else, use $make -f [makefilename]
- Always use the full variable expansion syntax i.e. $(VARIABLE) or make may not output the commands you want.
- make can work recursively, so if you have a bunch of sub-modules in your project that live inside of directories, you can call make on the sub-directory makefile from within make to build each.
- If you have a really complicated project that needs an installation scripts, etc. you'll probably also want to investigate autotools which generates the makefile for you and does a bunch of tricks to check for library existence and for other portability issues.