makefile notes - Rules Test

With this simple makefile file column

# 生成可执行文件main
main:main.o
    cc -o main main.o
# 生成main.o对象文件
main.o:main.c
    cc -c main.c
# 清除指令,删除执行过程中生成的文件

# 伪目标
.PHONY:clean

clean:
    rm main main.o

Rule format

target...:prerequisites...
<tab> command
  • target: target
  • prerequisites: dependent files
  • command: Command

1. explore the role of target and prerequisite of

  1. carried out make main.o
    • Run Log cc -c main.c
    • Main.o generated file
  2. Executed againmake mian.o
    • Run Log 'main.o' is up to date, it is up to date of the
  3. After modifying the file main.c
    • Run Log cc -c main.c
    • Main.o generated file
  4. Modify the makefile to
main.o:
    cc -c main.c
  1. carried out make mian.o
    • Run Log cc -c main.c
    • Main.o generated file
  2. carried out make mian.o
    • Run Log cc -c main.c
    • Main.o generated file
      6. Modify the makefile to
main:main.c
    cc -c main.c
  • 4,5 conclusions and results of the same

in conclusion

  1. When the target is the name of the target file is generated, the target will Makefile file name corresponding to the file dependent, and last modification time comparison, determines whether to execute the command to generate a new file, to avoid repeatedly performing
  2. Not set dependency file name is not a target or destination file name does not affect the target instruction execution

2. The problem of pseudo target

Some say pseudo target on the blog do not generate the target file is used

to clean the column above target

  • There are .PHONY:cleantime code executionmake clean
    • Delete command is executed, the file does not generate clean
  • Deleted .PHONY:cleanafter the code, execute commands, and no clean files

in conclusion

  1. Pseudotarget no effect
  2. When the target file name itself does not produce, not a destination file name, the name only serve as a method of action

3. With regard to obscure rule

  1. Delete the code
main.o:main.c
    cc -c main.c
  1. Execution make
    • Journalcc -c -o main.o main.c
    • Journalcc -o run main.o

Conclusion
C compilation process, when there are .o file depends on if there is no explicit rule generates .o file, makefile will auto-complete command generating .o


ps: If you have questions and would like to welcome the king of mutual encouragement exchange


Reproduced in: https: //www.jianshu.com/p/e521f09d1b28

Guess you like

Origin blog.csdn.net/weixin_34080903/article/details/91306922