make 命令 & makefile

makefile syntax

  • dependencies (相依性項目)
    • one target (目標項目)
    • some source code files
    • (target), (semicolon), (space or tab), ....., (source code files), (space or tab), .....
    • e.g. all: myapp myapp.l
  • rules (法則)
    • the rules illustrate how to generate a target
    • (tab)
    • the rules muse be on the same line
  • comment (註解)
    • (#)
  • macro (巨集)
    • define the macro => MACRONAME = value
    • access the macro => $(MACRONAME), ${MACRONAME}
  • -
    • make will ignore the error
    • e.g. -mkdir
    • e.g. -rm -r
  • @
    • make will not output the executed command on standard output
  • #這是makefile的格式(註解) 
    [target]: [dependency] [dependency] 
    [TAB][rule] 
    [TAB][rule] 
    [target]: [dependency] 
    [TAB][rule] 

make options


-k : 會讓make再遇到錯誤之時,仍然繼續執行而不會停在第一個問題點。可以利用這個選項找出發生問題的原始碼檔案。

-n : 告訴make只列出要進行的工作,而不會真正去執行它。

-f : tell the make to use the as a makefile. Otherwise, the make will use the makefile, Makefile (recommend).

-p : 要求 make 印出內建法則

-j3 : use the 3 cores 

Automatic Varaiables


$@ :  The name of the current target 

$% :  The filename element of an archive member specification

$<: first="" he="" name="" nbsp="" of="" p="" prerequisite="" the="">
$?:    The names of all prerequisites that are newer than the target, separated by spaces,代表須要重建(被修改)的相依性項目

$^:     The names of all prerequisites, separated by spaces. This is list has duplicated names removed.

$+:     Similar to $^, except $+ includes duplicateds.. This variable was created for specific situations such as arguments to linkers where duplicated values have meaning.

$*:    The stem of the target filename. A stem is typically a filename without its suffix. Current dependencies without the extension (副檔名)


  1. define the marcro : 1) make CC=c89    2) make "CC = c89"
  2. Use the all as the first target in makefile
  3. all (target)
  4. install (target)
  5. uninstall (target)
  6. clean (target)


GNU gcc

gcc -MM : generate a dependency list to make


Conventional Macros (common built in rules of makefiles)

typing "make −p" to print out the defaults
ARArchive−maintaining program; default is `ar'.
ASProgram for compiling assembly files; default is `as'.
CCProgram for compiling C programs; default is `cc'.
COProgram for checking out files from RCS; default is `co'.
CXXProgram for compiling C++ programs; default is `g++'.
CPPProgram for running the C preprocessor, with results to standard output; default is `$(CC) -E'.
ARFLAGSFlags to give the archive-maintaining program; default is `rv'.
ASFLAGSExtra flags to give to the assembler when explicitly invoked on a `.s' or `.S' file.
CFLAGSExtra flags to give to the C compiler.
CXXFLAGSExtra flags to give to the C compiler.
COFLAGSExtra flags to give to the RCS co program.
CPPFLAGSExtra flags to give to the C preprocessor and programs, which use it (such as C and Fortran compilers).

Conditional Directives

  • ifeq
  • ifneq
  • ifdef
  • ifndef
  • else
  • endif
e.g.
ifeq (arg1, arg2)
ifeq 'arg1' 'arg2'
ifeq "arg1" "arg2"
ifeq "arg1" 'arg2'
ifeq 'arg1' "arg2" 

相關文件