Makefile-只修改了.h頭文件,編譯為什么不起作用?
不知道各位小伙伴是否碰到過這樣的情況:
一個(gè) .c 文件 include 另一個(gè) .h 頭文件,使用 Makefile 來構(gòu)建(編譯)應(yīng)用程序。
第一次編譯、執(zhí)行,很正常!
但是此時(shí),如果修改了 .h 頭文件,再次編譯時(shí),就出現(xiàn)問題了:
預(yù)期的執(zhí)行流程是:make 發(fā)現(xiàn) .h 頭文件的修改時(shí)間更新,于是重新編譯包含這個(gè)頭文件的所有 .c 文件。
可實(shí)際的結(jié)果卻是:make 并沒有識別出 .h 頭文件的修改。
這是怎么回事呢?讓我們一一道來。
簡單的代碼示例
一個(gè)頭文件:hello.h
- #ifndef _HELLO_
- #define _HELLO_
- #define NUM 1
- #endif
一個(gè)源文件:main.c
- #include <stdio.h>
- #include "hello.h"
- int main(int argc, char *agv[])
- {
- printf("NUM = %d \n", NUM);
- return 0;
- }
Makefile 文件:
- OBJS := main.o
- TARGET := main
- all : $(OBJS)
- gcc -o $(TARGET) $(OBJS)
- %.o: %.c
- gcc $< -c -o $@
現(xiàn)在我們來第一次執(zhí)行 make,編譯一下:
- $ make
- gcc main.c -c -o main.o
- gcc -o main main.o
執(zhí)行一下:
- $ ./main
- NUM = 1
我們現(xiàn)在把 hello.h 文件中的 NUM 改成 2,現(xiàn)在的文件修改時(shí)間是:
- $ ll
- total 28
- -rw-rw-r-- 1 root root 58 Jun 7 20:52 hello.h
- -rwxrwxr-x 1 root root 8608 Jun 7 20:51 main*
- -rw-rw-r-- 1 root root 122 Jun 7 20:51 main.c
- -rw-rw-r-- 1 root root 1528 Jun 7 20:51 main.o
- -rw-rw-r-- 1 root root 100 Jun 7 20:51 Makefile
然后再執(zhí)行 make 指令,編譯一下:
- $ make
- gcc -o main main.o
可以看到:make 只執(zhí)行了 Makefile 中的鏈接指令(從目標(biāo)文件 main.o 到可執(zhí)行文件 main),并沒有執(zhí)行 gcc main.c -c -o main.o 這條編譯指令來重新編譯目標(biāo)文件。
也就說明:make 并沒有識別出 hello.h 這個(gè)頭文件已經(jīng)被改動了,盡管它“應(yīng)該”可以從文件的修改時(shí)間上發(fā)現(xiàn)!
為什么會這樣?
我們來看一下 Makefile 中的這個(gè)規(guī)則:
- %.o: %.c
- gcc $< -c -o $@
目標(biāo)文件 main.o,只是依賴了 main.c 文件,并沒有依賴 hello.h 文件。
make 的執(zhí)行規(guī)則是:只有目標(biāo)文件不存在,或者依賴文件比目標(biāo)文件更新的時(shí)候,才會執(zhí)行編譯指令。
因此,雖然 hello.h 被修改了,但是它并不是目標(biāo)文件 main.o 的依賴。
make 發(fā)現(xiàn):main.o 在當(dāng)前目錄中是已經(jīng)存在的,并且它比 main.c 更新,因此不會重新編譯 main.o。
所以即使 hello.h 被修改了,也不會起作用,因?yàn)?make 壓根就不把 hello.h 當(dāng)做 main.o 的依賴!
注意:所有的操作過程沒有執(zhí)行 clean 操作。
最簡單、無腦的方法
既然知道了原因,那就好辦了,我們手動把頭文件 hello.h 加到依賴中,不就可以了嗎?!
把 Makefile 中最后面幾句修改成下面這樣:
- HEADERS := hello.h
- %.o: %.c ${HEADERS}
- gcc $< -c -o $@
也就是把 .h 文件,也加入到 .o 文件的依賴中,這樣的話,每次修改 .h 文件后,再執(zhí)行 make 指令時(shí),就可以重新編譯 .o 目標(biāo)文件了。
您可試一下,這樣做肯定是沒有問題的。
到此,問題是被解決了,但是總覺得這樣的方式比較粗魯。
想一下:如果有很多的 .c 和 .h 文件呢,總不能手動一個(gè)一個(gè)添加吧?
高級一點(diǎn)的方法
修改 Makefile 為下面這樣:
- OBJS := main.o
- TARGET := main
- all : $(OBJS)
- gcc -o $(TARGET) $(OBJS)
- -include *.d
- %.o: %.c
- gcc $< -c -MMD -o $@
改動部分有 2 處:
1. 添加了 -include *.d 指令;
2. gcc 編譯指令中,添加了 -MMD 參數(shù);
我們先執(zhí)行一下試試。第一次編譯:
- $ ll // 查看當(dāng)前文件
- total 12
- -rw-rw-r-- 1 root root 58 Jun 7 21:06 hello.h
- -rw-rw-r-- 1 root root 122 Jun 7 20:51 main.c
- -rw-rw-r-- 1 root root 119 Jun 7 21:05 Makefile
- $
- $ make // 編譯
- gcc main.c -c -MMD -o main.o
- gcc -o main main.o
- $
- $ ll // 再次查看當(dāng)前文件
- total 32
- -rw-rw-r-- 1 root root 58 Jun 7 21:06 hello.h
- -rwxrwxr-x 1 root root 8608 Jun 7 21:06 main*
- -rw-rw-r-- 1 root root 122 Jun 7 20:51 main.c
- -rw-rw-r-- 1 root root 23 Jun 7 21:06 main.d
- -rw-rw-r-- 1 root root 1528 Jun 7 21:06 main.o
- -rw-rw-r-- 1 root root 119 Jun 7 21:05 Makefile
- $
- $ ./main // 執(zhí)行
- NUM = 1
有沒發(fā)現(xiàn):多出了一個(gè)文件 main.d,該文件內(nèi)容是:
- main.o: main.c hello.h
這個(gè)文件正是因?yàn)?Makefile 中的 -MMD 這個(gè)參數(shù)導(dǎo)致生成的,而它的內(nèi)容正是我們需要的目標(biāo)文件依賴信息。
然后在 Makefile 中,include 這個(gè) .d 文件,從而讓 make 知道:main.o 文件依賴于 main.c 和 hello.o 這 2 個(gè)文件。
這個(gè)時(shí)候,我們再來修改 hello.h 中的內(nèi)容,例如:把 NUM 改成 10,再次編譯、執(zhí)行:
- $ make
- gcc main.c -c -MMD -o main.o
- gcc -o main main.o
- $
- $ ./main
- NUM = 10