GNU Make Notes

plus2047 于 2021-10-19 发布

Basic

The base grammer of command make just like:

wanted_file1: depended_file1, dep2,dep3
<tab>command1
<tab>command2
wanted_file2: dep1,dep2
<tab>command1
<tab>command2
all:
<tab>command...
clean:
<tab>command...

It means if you want file1, you need dep1, dep2, dep3. If anyone of it have been changed when make running, command1,command2… will be run to get file1. WARNING: make don’t know how to generate file1 and it just run the command you write.

Some ‘filename’ is special. Commands after all will be run in any situation because there are no dependence after it. Command after clean will be run when you run make clean. i.e. perhaps we can see them as normal files.

If you run make command, any files without dependence and files whose dependence have been changed will be generate by the commands after it. if you run make filename, just the filename will be generate.

Variable

You can use variable in makefile. read it by $(a) and define it by b=$(a), in this case the value of b will be calculate when you use it. Or define it by b:=$(b), and the value will be calculate immediately.

Function

You can also use function in makefile. use it just like $(shell clear), it means use ‘clear’ string as param to run ‘shell’ function. (this function is a build-in function, which will run command in system

TODO: user-defined function