makefile

Reference from:史上最强最细腻的linux嵌入式C语言学习教程【李慧芹老师】_哔哩哔哩_bilibili   p70

​​​​​​​

 

And: Sorry about my Chinglish cuz there has not a chinese input language in ubuntu os...XD

****
original version 1: 
****
mytool:main.o tool1.o tool2.o
    gcc main.o tool1.o tool2.o -o mytool

main.o:main.c
    gcc main.c -c -Wall -g -o main.o 
too1.o:tool1.c
    gcc tool1.c -c -Wall -g -o tool1.c
too2.o:tool2.c
    gcc tool2.c -c -Wall -g -o tool2.c

clean:
    rm *.o mytool  -rf
    
-Wall -g : warning all and debug
-----------------------------------------------------
// define phase 

//use A=a to define one or several project names\chars
OBJS=main.o tool1.o tool2.o
CC=gcc 
// CC default = gcc , you can not to type "CC = gcc"


//Complie phase 

mytool:$(OBJS)
    $(CC) $(OBJS) -o mytool
// use $(A) to replace the original chars 通配符
// $(CC) translate to gcc
// $(OBJS) translate to main.o tool1.o tool2.o

main.o:main.c
    $(CC) main.c -c -Wall -g -o main.o 
too1.o:tool1.c
    $(CC) tool1.c -c -Wall -g -o tool1.c
too2.o:tool2.c
    $(CC) tool2.c -c -Wall -g -o tool2.c

clean:
    //rm *.o mytool  -rf
    $(RM) *.o mytool -r
//make tool defined RM = rm -f , so we use $(RM) to replace rm -f 

****
so version2:
****

OBJS=main.o tool1.o tool2.o
CC=gcc 

mytool:$(OBJS)
    $(CC) $(OBJS) -o mytool

main.o:main.c
    $(CC) main.c -c -Wall -g -o main.o 
too1.o:tool1.c
    $(CC) tool1.c -c -Wall -g -o tool1.c
too2.o:tool2.c
    $(CC) tool2.c -c -Wall -g -o tool2.c

clean:    
    $(RM) *.o mytool -r
-----------------------------------------------------
// we can easily see that there are idential -c -Wall -g in the several lines
// so we can also type an equaltaiton to define the chars
// in the define phase we usrally use CFLAGS to replace the appendence 
// CFLAGS +=-c -Wall -g 


OBJS=main.o tool1.o tool2.o
CC=gcc 
CFLAGS +=-c -Wall -g 

mytool:$(OBJS)
    $(CC) $(OBJS) -o mytool 
// Addition $(OBJS) appeared on previous line, usually we use "^" to instead the previous-line-appeared chars, and obviously this replacement is absolutely obviously.(XD)
// so them turning to :

mytool:$(OBJS)
    $(CC) $^ -o mytool 
// and the target filename, in this exam is the "mytool" and it's also a relative name and usually the name is same as the original name not include the suffix. we use "@" to relace.Do Not forget the "$".:)
// so them turning to :

mytool:$(OBJS)
    $(CC) $^ -o $@ 

main.o:main.c
    $(CC) $^ $(CFLAGS) -o $@
too1.o:tool1.c
    $(CC) $^ $(CFLAGS) -o $@
too2.o:tool2.c
    $(CC) $^ $(CFLAGS) -o $@

clean:    
    $(RM) *.o mytool -r

****
so version3:
****

OBJS=main.o tool1.o tool2.o
CC=gcc 
CFLAGS +=-c -Wall -g 

mytool:$(OBJS)
    $(CC) $^ -o $@ 

main.o:main.c
    $(CC) $^ $(CFLAGS) -o $@
too1.o:tool1.c
    $(CC) $^ $(CFLAGS) -o $@
too2.o:tool2.c
    $(CC) $^ $(CFLAGS) -o $@

clean:    
    $(RM) *.o mytool -r
-----------------------------------------------------
// AND!
// From now on! we got a same line several times!
// "$(CC) $^ $(CFLAGS) -o $@" this line 
// AND!
// We found a regulation of the phase two.
// We compile base on the same rule: 
//    XX.o:XX.c
//          $(CC) $^ $(CFLAGS) -o $@
// CC=gcc 
//The char "%" will replace to the (OBJS)'s arrtribution respectively.


****
version 4:
****

OBJS=main.o tool1.o tool2.o
CFLAGS +=-c -Wall -g 

mytool:$(OBJS)
    $(CC) $^ -o $@ 

%.o:%.c   
    $(CC) $^ $(CFLAGS) -o $@

clean:    
    $(RM) *.o mytool -r
-----------------------------------------------------
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值