[英]Makefile on GCC for C
我正在使用MinGW並嘗試使用Makefile創建合理的可執行文件,但它給了我以下錯誤:
Directory of C:\Users\PATTY\Desktop\loops
08/19/2016 01:34 PM <DIR> .
08/19/2016 01:34 PM <DIR> ..
08/18/2016 10:41 PM 59,628 a.exe
08/18/2016 11:58 PM 261 demo.c
08/18/2016 11:59 PM 59,541 demo.exe
08/18/2016 07:01 PM 605 justify.c
08/18/2016 07:01 PM 1,122 line.c
08/18/2016 07:02 PM 197 line.h
08/19/2016 01:03 AM 350 newquote.txt
08/18/2016 11:05 PM 628 planets.c
08/18/2016 11:14 PM 60,139 planets.exe
07/15/2016 08:03 PM 89 pun.c
08/19/2016 12:55 AM 412 quote.txt
08/13/2016 05:32 PM 167 reverse.c
08/18/2016 10:45 PM 28,107 reverse.exe
08/18/2016 10:45 PM 708 reverse.o
08/19/2016 03:14 AM 459 something.txt
08/11/2016 11:14 PM 1,698 test.c
08/18/2016 07:01 PM 427 word.c
08/18/2016 07:02 PM 92 word.h
18 File(s) 214,630 bytes
2 Dir(s) 164,884,508,672 bytes free
C:\Users\PATTY\Desktop\loops>mingw32-make justify
cc justify.c -o justify
process_begin: CreateProcess(NULL, cc justify.c -o justify, ...) failed.
make (e=2): The system cannot find the file specified.
<builtin>: recipe for target 'justify' failed
mingw32-make: *** [justify] Error 2
C:\Users\PATTY\Desktop\loops>
我是一個初學者,所以請讓我知道我在想什么或做錯了什么。
編輯:
另一個失敗的嘗試:
C:\Users\PATTY\Desktop\loops>mingw32-make justify CC=gcc
gcc justify.c -o justify
C:\Users\PATTY\AppData\Local\Temp\cc2z2lvl.o:justify.c:(.text+0x10): undefined reference to `clear_line'
C:\Users\PATTY\AppData\Local\Temp\cc2z2lvl.o:justify.c:(.text+0x24): undefined reference to `read_word'
C:\Users\PATTY\AppData\Local\Temp\cc2z2lvl.o:justify.c:(.text+0x40): undefined reference to `flush_line'
C:\Users\PATTY\AppData\Local\Temp\cc2z2lvl.o:justify.c:(.text+0x5f): undefined reference to `space_remainding'
C:\Users\PATTY\AppData\Local\Temp\cc2z2lvl.o:justify.c:(.text+0x68): undefined reference to `write_line'
C:\Users\PATTY\AppData\Local\Temp\cc2z2lvl.o:justify.c:(.text+0x6d): undefined reference to `clear_line'
C:\Users\PATTY\AppData\Local\Temp\cc2z2lvl.o:justify.c:(.text+0x79): undefined reference to `add_word'
collect2.exe: error: ld returned 1 exit status
<builtin>: recipe for target 'justify' failed
mingw32-make: *** [justify] Error 1
C:\Users\PATTY\Desktop\loops>
我正在使用MinGW並嘗試使用Makefile創建合理的可執行文件
不你不是。 make命令:
C:\Users\PATTY\Desktop\loops>mingw32-make justify
沒有指定任何makefile。 在這種情況下,默認情況下, make
將在工作目錄中查找makefile
,否則, make
將在工作目錄中查找Makefile
。 但是C:\\Users\\PATTY\\Desktop\\loops
的目錄列表顯示,這些makefile都不存在。
在這種情況下, make
會退回到其內置規則數據庫,以查看是否有任何規則可以讓它在沒有任何指定或默認makefile的情況下,根據工作目錄中存在的文件來構建目標justify
。
它找到以下內置規則:
%: %.c
$(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@
它將嘗試從單個匹配的C源文件%.c
編譯並鏈接目標匹配的%
。
此規則與目標justify
和源文件justify.c
相匹配。 和源文件存在於工作目錄,這樣make
嘗試的食譜:
$(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@
前提條件$^
= justify.c
,目標$@
= justify
但這不起作用,因為在完全擴展變量之后,配方變為:
cc justify.c -o justify
其中cc
是make變量CC
的默認值,表示C編譯器。 這是因為定義了$(LINK.c)
:
LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
您的PATH
沒有cc
這樣的程序,因此該配方失敗:
process_begin: CreateProcess(NULL, cc justify.c -o justify, ...) failed.
make (e=2): The system cannot find the file specified.
您意識到這是問題的本質,然后嘗試:
C:\Users\PATTY\Desktop\loops>mingw32-make justify CC=gcc
有這樣一個程序作為gcc
在你的PATH
,這是你的C編譯器。 更好,但是內置配方現在擴展為:
gcc justify.c -o justify
它試圖從單個源文件justify.c
編譯並鏈接程序justify
。 如您所知 ,在此目錄中構建此程序所需的配方為:
gcc -o justify justify.c line.c word.c
因此,由於您沒有編譯或鏈接定義了缺少功能的源文件,因此您現在運行的配方因觀察到的鏈接錯誤而失敗。
如果要使用make
正確構建程序,則需要學習編寫makefile的要點,然后編寫一個指示make
正確構建程序的內容。 您可以將此makefile保存為C:\\Users\\PATTY\\Desktop\\loops
中的makefile
或Makefile
,默認情況下make
將使用它。 或者,您可以隨意調用它,並在調用make
時使用-f
選項按名稱指定它:
mingw32-make -f whatever.mak ...
這是將GCC與GNU make一起使用的相當不錯的初學者教程 。 對於權威文檔, 這是GCC手冊 , 這是GNU Make手冊
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.