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