[英]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.