Re: Makefile的gcc加入-I沒做用 - Linux

Table of Contents

※ 引述《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/

--

All Comments

Puput avatarPuput2008-12-19
我把hello.o去掉還是不行
Callum avatarCallum2008-12-23
%.o: %.c 這個就ok
Sarah avatarSarah2008-12-27
改成hello.c才對 XD
Agnes avatarAgnes2008-12-28
改成hello.c的話,只會出現make 'main' is up to date
Aaliyah avatarAaliyah2009-01-01
有產生binary卻無法產生.o檔
Yuri avatarYuri2009-01-03
之前的main檔案要刪掉 ... 沒有.o有什麼不正常的地方嗎@@?
Heather avatarHeather2009-01-08
不習慣而已,感謝感謝
Skylar DavisLinda avatarSkylar DavisLinda2009-01-13
對了,沒有compile的過程顯示在shell也覺得怪怪的
Sarah avatarSarah2009-01-17
雖然刪掉後 make後有出現main但顯示的卻是up to date訊息
Iris avatarIris2009-01-18
好像沒compile似的...~~
Hedy avatarHedy2009-01-23
因為沒有相依的檔案,你的action不能用$<輸入,必須寫死