Makefile: Introduction to basic

Makefile: Introduction to basic

This is a very simple demo about makefile.

How does the makefile and make works?

edit : main.o kbd.o command.o display.o insert.o search.o files.o utils.o
       cc -o edit main.o kbd.o command.o display.o insert.o search.o files.o utils.o
       
main.o : main.c defs.h
  cc -c main.c
kbd.o : kbd.c defs.h command.h
  cc -c kbd.c
command.o : command.c defs.h buffer.h
  cc -c command.c
display.o : display.c defs.h buffer.h
  cc -c display.c
insert.o : insert.c defs.h buffer.h
  cc -c insert.c
search.o : search.c defs.h buffer.h
  cc -c search.c
files.o : files.c defs.h buffer.h command.h
  cc -c files.c
utils.o : utils.c defs.h
  cc -c utils.c

clean : 
  rm edit main.o kbd.o command.o display.o insert.o search.o files.o utils.o

When we call the make command, the target file is edit, and the dependent file is main.o kbd.o command.o display.o insert.o search.o files.o utils.o. If main.o is not existed, main.o file will be created by cc -c main.c command. The dependent file of main.o is main.c and defs.h. The followed dependent file of edit will be created as the same way if they are not exited. If the dependent file change, the target file will be created again when make is called.
When we call the make clean, the shell will run the command under clean : rm edit main.o kbd.o command.o display.o insert.o search.o files.o utils.o. \

Use the variable and auto-derivation.

object = main.o kbd.o command.o display.o insert.o search.o files.o utils.o
edit : $(object)
       cc -o $(object)
       
main.o :  defs.h
kbd.o : defs.h command.h
command.o : defs.h buffer.h
display.o : defs.h buffer.h
insert.o : defs.h buffer.h
search.o : defs.h buffer.h
files.o : defs.h buffer.h command.h
utils.o : defs.h

.PHONY : clean
clean : 
  rm edit $(object)

This version will works as the upper makefile. The make command will automatically derivate command and find files needed when it need depented files. For example, when it created the main.o, the main.c will be found and cc will be called.
Using the varible will simply the the code. It is simple as I write in this demo.

Another style of makefiles

object = main.o kbd.o command.o display.o insert.o search.o files.o utils.o
edit : $(object)
       cc -o $(object)
       
$(object): defs.h
kbd.o command.o files.o : command.h
display.o insert.o files.o : buffer.h

.PHONY : clean
clean : 
  rm edit $(object)

Reference

How to write makefile Chen Hao

End

The brief introduction about makefile is over here.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值