当前位置:网站首页>c files always get rebuild when make -------- . PHONY in Makefile
c files always get rebuild when make -------- . PHONY in Makefile
2022-06-22 05:39:00 【alex_ mianmian】
.PHONY is key word in Makefile, it means a fake target.
Firstly let's list a Makefile as following:
C_FLAGS =
SOURCE = main.c test.c
OBJS_C = $(SOURCE:.c=.o)
DEP = $(SOURCE:.c=.d)
HEADER= header.h
mktest:$(OBJS_C)
@echo "objects is" "$(OBJS_C)"
gcc $(OBJS_C) -o mktest
include $(DEP)
.PHONY:$(HEADER)
$(HEADER):
@echo "test header cause rebuild"
%.o:%.c
@echo "start making objs"
gcc -c $< -o [email protected]
%.d:%.c
@echo "Making [email protected] ..."
@set -e;
gcc -E -MM $(filter %.c, $^) > [email protected]
clean:
-rm *.o
-rm *.d
-rm mktestDo you still remember yesterday's Makefile. I just added following rule.
HEADER= header.h
.PHONY:$(HEADER)
$(HEADER):
@echo "test header cause rebuild"This rule means what? let's explain later. Now let's create header.h and add it into main.c then run 'make'.
[email protected]:~/mktest$ make
Makefile:16: main.d: No such file or directory
Makefile:16: test.d: No such file or directory
Making test.d ...
gcc -E -MM test.c > test.d
Making main.d ...
gcc -E -MM main.c > main.d
test header cause rebuild
start making objs
gcc -c main.c -o main.o
start making objs
gcc -c test.c -o test.o
objects is main.o test.o
gcc main.o test.o -o mktestwe can see all source files get build. Now let's run 'make' again. What should we see? we didn't change anything, right? should be nothing build.
[email protected]:~/mktest$ make
test header cause rebuild
start making objs
gcc -c main.c -o main.o
objects is main.o test.o
gcc main.o test.o -o mktestWhat, main.o is built again and mktest get link again. Why?
Firstly let's check main.d:
main.o: main.c test.h other.h header.hheader.h is the depend file of main.o, when header.h get update, main.o get rebuild. But we didn't modify header.h
So why?
Yes, I didn't modify or update header.h, but header.h has rule in Makefile -- $(HEADER):
it has no depend file, how it get update? remember .PHONY:$(HEADER) ?
.PHONY can make the fake target run even if has no depend file.
Now let's clear the sequence of the first 'make':
1. -include $(DEP), try to add main.d and test.d to Makefile, not found them then check their rule
2. found %.d:%.c and create main.d and test.d, main.d add header.h as main.o's depend file
3. -include $(DEP) add main.d and test.d into Makefile, then main.o and test.o targets get in Makefile
4. main.o target has depend file header.h, and check header.h's rule
5. found $(HEADER): so we can see log 'test header cause rebuild'
6. build main.o and test.o by rule %.o:%.c
7. link mktest
The second 'make' starts from step 3 to step7. But test.o doesn't build as no depend file update and depend file has no rule in Makefile.
If we remove the .PHONY:$(HEADER) then do third 'make', what will happen? will header.h is updated? here is the result:
[email protected]:~/mktest$ make
make: 'mktest' is up to date.
[email protected]:~/mktest$Look, no building happen.
So can you understand .PHONY now?
And one important thing is: the real header.h never changed(ls -l can check the file date), but the header.h in Makefile get update by .PHONY rule.
This example is from a real bad project Makefile. it tells us don't do:
source file includes header file, but Makefile has header file fake target. In this case, fake target header file will cause source file get build everytime 'make'.
边栏推荐
- [cloud native] 2.2 kubeadm create cluster
- 《MATLAB 神经网络43个案例分析》:第28章 决策树分类器的应用研究——乳腺癌诊断
- Zhiyuan OA vulnerability analysis, utilization and protection collection
- 代码走查的优化方向(接口请求便捷、动态class判断条件过长、去除无用console、抽离公共方法)
- 数据的存储(进阶)
- Mobile terminal layout adaptation
- Global and Chinese digital imager market development outlook and investment competitiveness analysis report 2022-2027
- Opencv function usage details 1~10, including code examples
- A piece of code to solve the problem of automatic disconnection of Google colab
- printf becomes puts
猜你喜欢
随机推荐
Current market situation analysis and investment analysis prospect report of global and Chinese ceramic capacitor industry 2022-2027
[issue 26] 123hr experience of Tencent teg+ operation development
Working method: 3C scheme design method
Remove then add string from variable of Makefile
Delete the packaging use of pop-up components
Which is the trend of cross-border policy frequent adjustment of "independent stations & platforms"?
Shenzhen Nanshan District specialized special new small giant enterprise declaration index, with a subsidy of 500000 yuan
在线文本代码对比工具
Stockage des données (avancé)
C语言指针(进阶)
Conversion between JSON, string and map
Key points of Facebook account "unsealing, anti sealing and maintaining ID" have been collected!
How can enterprises implement ERP projects automatically?
P1077 [noip2012 popularization group] flower display
Want to put Facebook ads but don't know where to start? This article takes you to know more about
深圳南山区专精特新小巨人企业申报指标,补贴50万
tmux -- ssh terminal can be closed without impact the server process
open source hypervisor
count registers in C code -- registers has one pattern
Running yolov5 reports an error attributeerror: cant get attribute sppf on module models common




![Force buckle 33 [search rotation sort array]](/img/0f/d7e2f2fdf48bcd70e6c69bca7d4002.png)

![P1077 [NOIP2012 普及组] 摆花](/img/0d/f74a2036aa261ed327d9d74291aacc.png)

