自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Makefile-只修改了.h頭文件,編譯為什么不起作用?

開發(fā) 后端
不知道各位小伙伴是否碰到過這樣的情況:一個(gè) .c 文件 include 另一個(gè) .h 頭文件,使用 Makefile 來構(gòu)建(編譯)應(yīng)用程序。

[[404400]]

不知道各位小伙伴是否碰到過這樣的情況:

一個(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

  1. #ifndef _HELLO_ 
  2. #define _HELLO_ 
  3.  
  4. #define  NUM    1 
  5.  
  6. #endif 

一個(gè)源文件:main.c

  1. #include <stdio.h> 
  2. #include "hello.h" 
  3.  
  4. int main(int argc, char *agv[]) 
  5.     printf("NUM = %d \n", NUM); 
  6.     return 0; 

Makefile 文件:

  1. OBJS := main.o  
  2. TARGET := main 
  3.  
  4. all : $(OBJS)  
  5.     gcc -o $(TARGET) $(OBJS) 
  6.  
  7. %.o: %.c  
  8.     gcc $< -c -o $@ 

現(xiàn)在我們來第一次執(zhí)行 make,編譯一下:

  1. $ make 
  2. gcc main.c -c -o main.o 
  3. gcc -o main main.o 

執(zhí)行一下:

  1. $ ./main  
  2. NUM = 1 

我們現(xiàn)在把 hello.h 文件中的 NUM 改成 2,現(xiàn)在的文件修改時(shí)間是:

  1. $ ll 
  2. total 28 
  3. -rw-rw-r-- 1 root root   58 Jun  7 20:52 hello.h 
  4. -rwxrwxr-x 1 root root 8608 Jun  7 20:51 main* 
  5. -rw-rw-r-- 1 root root  122 Jun  7 20:51 main.c 
  6. -rw-rw-r-- 1 root root 1528 Jun  7 20:51 main.o 
  7. -rw-rw-r-- 1 root root  100 Jun  7 20:51 Makefile 

然后再執(zhí)行 make 指令,編譯一下:

  1. $ make 
  2. 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ī)則:

  1. %.o: %.c  
  2.     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 中最后面幾句修改成下面這樣:

  1. HEADERS := hello.h 
  2. %.o: %.c ${HEADERS} 
  3.     gcc $< -c -o $@ 

也就是把 .h 文件,也加入到 .o 文件的依賴中,這樣的話,每次修改 .h 文件后,再執(zhí)行 make 指令時(shí),就可以重新編譯 .o 目標(biāo)文件了。

您可試一下,這樣做肯定是沒有問題的。

到此,問題是被解決了,但是總覺得這樣的方式比較粗魯。

想一下:如果有很多的 .c 和 .h 文件呢,總不能手動一個(gè)一個(gè)添加吧?

高級一點(diǎn)的方法

修改 Makefile 為下面這樣:

  1. OBJS := main.o  
  2. TARGET := main 
  3.  
  4. all : $(OBJS)  
  5.     gcc -o $(TARGET) $(OBJS) 
  6.  
  7. -include *.d 
  8. %.o: %.c  
  9.     gcc $< -c -MMD -o $@ 

改動部分有 2 處:

1. 添加了 -include *.d 指令;

2. gcc 編譯指令中,添加了 -MMD 參數(shù);

我們先執(zhí)行一下試試。第一次編譯:

  1. $ ll      // 查看當(dāng)前文件 
  2. total 12 
  3. -rw-rw-r-- 1 root root  58 Jun  7 21:06 hello.h 
  4. -rw-rw-r-- 1 root root 122 Jun  7 20:51 main.c 
  5. -rw-rw-r-- 1 root root 119 Jun  7 21:05 Makefile 
  6. $  
  7. $ make    // 編譯 
  8. gcc main.c -c -MMD -o main.o 
  9. gcc -o main main.o 
  10. $  
  11. $ ll      // 再次查看當(dāng)前文件 
  12. total 32 
  13. -rw-rw-r-- 1 root root   58 Jun  7 21:06 hello.h 
  14. -rwxrwxr-x 1 root root 8608 Jun  7 21:06 main* 
  15. -rw-rw-r-- 1 root root  122 Jun  7 20:51 main.c 
  16. -rw-rw-r-- 1 root root   23 Jun  7 21:06 main.d 
  17. -rw-rw-r-- 1 root root 1528 Jun  7 21:06 main.o 
  18. -rw-rw-r-- 1 root root  119 Jun  7 21:05 Makefile 
  19. $  
  20. $ ./main  // 執(zhí)行 
  21. NUM = 1 

有沒發(fā)現(xiàn):多出了一個(gè)文件 main.d,該文件內(nèi)容是:

  1. 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í)行:

  1. $ make 
  2. gcc main.c -c -MMD -o main.o 
  3. gcc -o main main.o  
  4. $ ./main 
  5. NUM = 10 
本文轉(zhuǎn)載自微信公眾號「IOT物聯(lián)網(wǎng)小鎮(zhèn)」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系IOT物聯(lián)網(wǎng)小鎮(zhèn)公眾號。

 

責(zé)任編輯:武曉燕 來源: IOT物聯(lián)網(wǎng)小鎮(zhèn)
相關(guān)推薦

2020-05-26 15:53:01

StraceDocker容器

2021-05-07 07:50:44

Numactl內(nèi)存代碼

2021-04-06 11:46:18

比特幣加密貨幣資產(chǎn)

2020-07-23 08:17:47

代碼開發(fā)人員用戶

2024-06-04 16:42:45

2019-05-07 14:03:53

鼠標(biāo)中鍵Windows 10Windows

2022-12-01 08:09:05

SQLOracleSPM

2018-11-20 13:52:54

2023-03-20 09:42:06

2019-07-11 10:30:58

USBWindows 10 快速修復(fù)

2018-07-20 10:50:43

WindowsWindows 10Shift

2021-09-08 17:27:54

神經(jīng)網(wǎng)絡(luò)AI算法

2016-12-06 10:30:39

JavaScriptWriteWriteln

2019-03-13 10:45:21

憑據(jù)Windows 10遠(yuǎn)程桌面

2022-12-27 14:43:15

模型GPT

2009-12-17 15:18:47

2021-04-16 20:47:42

Go 指令函數(shù)

2009-09-22 11:24:07

Hibernate查詢

2013-04-28 13:54:26

Android編譯系統(tǒng)頭文件搜索路徑順序

2021-07-09 06:00:45

網(wǎng)絡(luò)釣魚培訓(xùn)數(shù)據(jù)泄露
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號