看板 Linux 關於我們 聯絡資訊
※ 引述《realmojo (( ̄▽ ̄)╭∩╮)》之銘言: : 我在bash下指令 : gcc hello.c -I./myinclude 是可以成功compile出a.out的 : 但是改在Makefile如下,打make,就會出現找不到.h檔 No Such File 的問題 : main: hello.o : gcc -Wall -o $@ $^ -I./myinclude : 該如何改? 我假設你的Makefile只有上述的那兩行 XD main: hello.o gcc -Wall -o $@ $^ -I./myinclude ^^^^^^^^^^^^^ 這個時候用 -I其實沒有效果,因為你這行實際上是做linking 根據你寫的規則,main這個target需要hello.o,但是你沒有描述他是怎麼產生的。 所以make他用內建的 %.o: %.c rule 來產生需要的 hello.o。 然而,內建的rule並沒有包含 -I./myinclude這個參數,因此會找不到需要的 header。 改寫成這樣: %.o: %.c $(CC) -c $< -o $@ -Wall -I./myinclude main: hello.o $(CC) $^ -o $@ -Wall 或者乾脆把main:hello.o 的hello.o去掉應該都會work。 有錯的話請不吝指教,謝謝。 -- Coding 日誌 & Linux 使用心得 Rinoworks Blog http://rinoworks.blogspot.com/ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.121.197.32
realmojo:我把hello.o去掉還是不行 12/15 12:01
realmojo:%.o: %.c 這個就ok 12/15 12:03
Bencrie:改成hello.c才對 XD 12/15 14:33
realmojo:改成hello.c的話,只會出現make 'main' is up to date 12/15 16:20
realmojo:有產生binary卻無法產生.o檔 12/15 16:21
Bencrie:之前的main檔案要刪掉 ... 沒有.o有什麼不正常的地方嗎@@? 12/15 17:02
realmojo:不習慣而已,感謝感謝 12/15 17:59
realmojo:對了,沒有compile的過程顯示在shell也覺得怪怪的 12/15 18:06
realmojo:雖然刪掉後 make後有出現main但顯示的卻是up to date訊息 12/15 18:07
realmojo:好像沒compile似的...~~ 12/15 18:08
Bencrie:因為沒有相依的檔案,你的action不能用$<輸入,必須寫死 12/15 19:56