2021-01-29

这篇博客详细介绍了Makefile的使用,包括显示规则、变量、隐含规则、通配符和函数等五个层次。作者通过实例展示了如何编译源代码文件,清理目标文件,并解释了Makefile中的一些常见指令和技巧。此外,还提到了Makefile中的变量、隐含规则简化以及通配符和函数在提高效率方面的作用。
摘要由CSDN通过智能技术生成

Makefile笔记-初学者


一:第一层:显示规则

1.目标文件:依赖文件
    {tab}指令
2.第一个目标文件是我的最终目标
3.伪目标rm -rf hello   .PHONY:

井号是注释
clearall和clear是自己起名字,尽量有意义

.PHONY:
clearall:
    rm -rf hello hello.o hello.S hello.i
clear:
    rm -rf hello.o hello.S hello.i

第一个目标文件是我的最终目标

hello.i:hello.c
    gcc -E hello.c -o hello.i

hello.S:hell.i
    gcc -S hello.i -o hello.S

hello.o:hello.S
    gcc -c hello.S -o hello.o

hello:hello.o
    gcc hello.o -o hello

按照《第一个目标文件是我的最终目标》这条规则,应该倒过来写
hello是可执行文件,类似递归,目标是得到hello,但是需要hello.o
下面有hello.o,但是得到hello.o需要hello.S,下面。。。。
Makefile

hello:hello.o
    gcc hello.o -o hello
hello.o:hello.S
    gcc -c hello.S -o hello.o
hello.S:hello.i
    gcc -S hello.i -o hello.S
hello.i:hello.c
    gcc -E hello.c -o hello.i

有Makefile文件直接在当前目录
make


Makefile

hello:hello.o
    gcc hello.o -o hello
hello.o:hello.S
    gcc -c hello.S -o hello.o
hello.S:hello.i
    gcc -S hello.i -o hello.S
hello.i:hello.c
.PHONY:
clear:
    rm -rf hello hello.o hello.S hello.i

shell执行make clear

Makefile

#circle.c circel.h cube.c cube.h main.c main.h
#得到可执行文件test,省去预处理,编译,汇编
test:circle.o cube.o main.o
    gcc circle.o cube.o main.o -o test
circle.o:circle.c
    gcc -c circle.c -o circle.o
cube.o:cube.c
    gcc -c cube.c -o cube.o
main.o:main.c
    gcc -c main.c -o main.o
.PHONY:
clearall:
    rm -rf circle.o cube.o main.o test
clear:
    rm -rf circle.o cube.o main.o


二:第二层:变量

-----------------------  =(替换)+=(追加):= (恒等于(常量))
使用变量 $(变量名)
TAR = test
TAR += test1
cc := gcc
obj = circle.o cube.o main.o

Makefile文件内容

TAR = test
OBJ = circle.o cube.o main.o
CC := gcc
$(TAR):$(OBJ)
    $(CC) $(OBJ) -o $(TAR)
circle.o:circle.c
    $(CC) -c circle.c -o circle.o
cube.o:cube.c
    $(CC) -c cube.c -o cube.o
main.o:main.c
    $(CC) -c main.c -o main.o
.PHONY:
clearall:
    rm -rf $(OBJ) $(TAR)
clear:
    rm -rf $(OBJ)


三:第三层:隐含规则


%.c %.o *.c *.o
任意的.c,任意的.o, 所有的.c,所有的.o 文件
100个.o文件怎么办?

这段代码

circle.o:circle.c
    $(CC) -c circle.c -o circle.o
cube.o:cube.c
    $(CC) -c cube.c -o cube.o
main.o:main.c
    $(CC) -c main.c -o main.o


可以简写为

%.o:%.c
    $(CC) -c %.c -o %.o


四:第四层: 通配符

 

$^ 所有的依赖文件
$@ 所有的目标文件
$< 所有的依赖文件的第一个文件
等等

这段代码

$(TAR):$(OBJ)
    $(CC) $(OBJ) -o $(TAR)
%.o:%.c
    $(CC) -c %.c -o %.o


可以简写为

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

Makefile文件内容

TAR=test
OBJ=circle.o cube.o main.o
CC:=gcc
RMRF:=rm -rf
$(TAR):$(OBJ)
    $(CC) $^ -o $@
%.o:%.c
    $(CC) -c $^ -o $@
.PHONY:
clearall:
    rm -rf $(OBJ) $(TAR)
clear:
    rm -rf $(OBJ)


五:第五层: 函数

 

视频链接:https://b23.tv/BV1B4411F7EK

以下是一个可能的Java实现: ```java import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.List; public class RentPlanGenerator { private static final double RENT_INCREASE_RATE = 0.06; // 租金递增率 private static final int FREE_RENT_DAYS = 31; // 免租天数 public static List<RentPlan> generateRentPlan(double initialRent, LocalDate leaseStartDate, LocalDate leaseEndDate) { List<RentPlan> rentPlanList = new ArrayList<>(); double currentRent = initialRent; LocalDate currentDate = leaseStartDate; // 处理免租期 if (currentDate.isBefore(leaseStartDate.plusDays(FREE_RENT_DAYS))) { currentDate = leaseStartDate.plusDays(FREE_RENT_DAYS); } while (currentDate.isBefore(leaseEndDate)) { LocalDate nextIncreaseDate = currentDate.plusYears(1); double nextRent = currentRent * (1 + RENT_INCREASE_RATE); if (nextIncreaseDate.isBefore(leaseStartDate.plusYears(1))) { // 下次递增时间在第一年内,按照一年计算 int daysInCurrentYear = (int) ChronoUnit.DAYS.between(currentDate, nextIncreaseDate); rentPlanList.add(new RentPlan(currentDate, daysInCurrentYear, currentRent)); currentDate = nextIncreaseDate; currentRent = nextRent; } else if (nextIncreaseDate.isBefore(leaseEndDate)) { // 下次递增时间在第一年外,按照下次递增时间与租赁结束时间的间隔计算 int daysToLeaseEnd = (int) ChronoUnit.DAYS.between(currentDate, leaseEndDate); rentPlanList.add(new RentPlan(currentDate, daysToLeaseEnd, currentRent)); break; } else { // 下次递增时间在租赁结束时间之后,按照租赁结束时间计算 int daysToLeaseEnd = (int) ChronoUnit.DAYS.between(currentDate, leaseEndDate); rentPlanList.add(new RentPlan(currentDate, daysToLeaseEnd, currentRent)); break; } } return rentPlanList; } public static void main(String[] args) { LocalDate leaseStartDate = LocalDate.of(2021, 3, 1); LocalDate leaseEndDate = LocalDate.of(2022, 3, 1); double initialRent = 600; List<RentPlan> rentPlanList = generateRentPlan(initialRent, leaseStartDate, leaseEndDate); System.out.printf("%-12s%-12s%-12s%n", "时间", "天数", "租金"); for (RentPlan rentPlan : rentPlanList) { System.out.printf("%-12s%-12d%-12.2f%n", rentPlan.getStartDate(), rentPlan.getDays(), rentPlan.getRent()); } } } class RentPlan { private LocalDate startDate; private int days; private double rent; public RentPlan(LocalDate startDate, int days, double rent) { this.startDate = startDate; this.days = days; this.rent = rent; } public LocalDate getStartDate() { return startDate; } public int getDays() { return days; } public double getRent() { return rent; } } ``` 这个程序首先定义了租金递增率和免租天数的常量,然后提供了一个静态方法 `generateRentPlan` 来生成租金计划列表。该方法接受三个参数:初始月租金、租赁开始时间和租赁结束时间。 具体实现时,我们使用循环来逐月生成租金计划。在每次循环中,我们首先计算下次递增租金的时间和金额。然后根据下次递增时间与租赁开始时间的间隔,决定本次循环处理的天数和租金金额。最后将这些信息保存到一个 `RentPlan` 对象中,并添加到租金计划列表中。 在主函数中,我们使用 `generateRentPlan` 方法生成租金计划列表,并以表格形式输出。输出结果如下: ``` 时间 天数 租金 2021-04-01 30 600.00 2021-05-01 31 636.00 2021-06-01 30 674.16 2021-07-01 31 713.57 2021-08-01 31 754.29 2021-09-01 30 796.39 2021-10-01 31 840.94 2021-11-01 30 887.02 2021-12-01 31 934.72 2022-01-01 31 984.12 2022-02-01 28 1035.30 ``` 可以看到,程序正确地根据递增周期和递增率生成了每个月的租金计划,并且考虑了免租期的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值