[英]Makefile on GCC for C
I'm using MinGW and trying to create a justify executable using the Makefile but it gives me the following error: 我正在使用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>
I'm a beginner when it comes to this so please let me know what am I missing or doing wrong. 我是一个初学者,所以请让我知道我在想什么或做错了什么。
Edit: 编辑:
Another failed attempt: 另一个失败的尝试:
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>
I'm using MinGW and trying to create a justify executable using the Makefile
我正在使用MinGW并尝试使用Makefile创建合理的可执行文件
No you're not. 不你不是。 The make command:
make命令:
C:\Users\PATTY\Desktop\loops>mingw32-make justify
doesn't specify any makefile. 没有指定任何makefile。 In that case,
make
will look by default for makefile
in the working directory, and failing that will look for Makefile
in the working directory. 在这种情况下,默认情况下,
make
将在工作目录中查找makefile
,否则, make
将在工作目录中查找Makefile
。 But the directory listing of C:\\Users\\PATTY\\Desktop\\loops
shows that neither of those makefiles exists either. 但是
C:\\Users\\PATTY\\Desktop\\loops
的目录列表显示,这些makefile都不存在。
In that case, make
falls back on its built-in rules database to see if any of them might let it build the target justify
from files that exist in the working directory, in the absence of any specified or default makefile. 在这种情况下,
make
会退回到其内置规则数据库,以查看是否有任何规则可以让它在没有任何指定或默认makefile的情况下,根据工作目录中存在的文件来构建目标justify
。
It finds this built-in rule: 它找到以下内置规则:
%: %.c
$(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@
which will attempt to compile and link a target matching %
from a single matching C source file %.c
. 它将尝试从单个匹配的C源文件
%.c
编译并链接目标匹配的%
。
This rule matches for target justify
and source file justify.c
. 此规则与目标
justify
和源文件justify.c
相匹配。 And that source file exists in the working directory, so make
tries the recipe: 和源文件存在于工作目录,这样
make
尝试的食谱:
$(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@
with prerequisite $^
= justify.c
and target $@
= justify
前提条件
$^
= justify.c
,目标$@
= justify
But it does not work because after full expansion of the variables, the recipe becomes: 但这不起作用,因为在完全扩展变量之后,配方变为:
cc justify.c -o justify
where cc
is the default value of the make variable CC
, denoting the C compiler. 其中
cc
是make变量CC
的默认值,表示C编译器。 That is because $(LINK.c)
is defined: 这是因为定义了
$(LINK.c)
:
LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
You have no such program as cc
in your PATH
, so the recipe fails: 您的
PATH
没有cc
这样的程序,因此该配方失败:
process_begin: CreateProcess(NULL, cc justify.c -o justify, ...) failed.
make (e=2): The system cannot find the file specified.
You realize this is the nature of the problem and you try: 您意识到这是问题的本质,然后尝试:
C:\Users\PATTY\Desktop\loops>mingw32-make justify CC=gcc
There is such a program as gcc
in your PATH
, and it is your C compiler. 有这样一个程序作为
gcc
在你的PATH
,这是你的C编译器。 That's better, but the built-in recipe now expands to: 更好,但是内置配方现在扩展为:
gcc justify.c -o justify
which attempts to compile and link your program justify
from the single source file justify.c
. 它试图从单个源文件
justify.c
编译并链接程序justify
。 And as you know , the recipe you require to build this program in this directory is: 如您所知 ,在此目录中构建此程序所需的配方为:
gcc -o justify justify.c line.c word.c
So the recipe you are running now fails with the linkage errors you have observed because you are not compiling or linking the source files in which the missing functions are defined. 因此,由于您没有编译或链接定义了缺少功能的源文件,因此您现在运行的配方因观察到的链接错误而失败。
If you want to build the program correctly using make
, you will need to learn the essentials of writing makefiles and then write one that instructs make
to build the program correctly. 如果要使用
make
正确构建程序,则需要学习编写makefile的要点,然后编写一个指示make
正确构建程序的内容。 You can save this makefile in C:\\Users\\PATTY\\Desktop\\loops
as either makefile
or Makefile
and make
will use it by default. 您可以将此makefile保存为
C:\\Users\\PATTY\\Desktop\\loops
中的makefile
或Makefile
,默认情况下make
将使用它。 Or you can call it whatever you like, and specify it by name with the -f
option when you invoke make
: 或者,您可以随意调用它,并在调用
make
时使用-f
选项按名称指定它:
mingw32-make -f whatever.mak ...
Here is a fairly sound beginner's tutorial for using GCC with GNU make . 这是将GCC与GNU make一起使用的相当不错的初学者教程 。 For authoritative documentation, here is the GCC manual and here is the GNU Make manual
对于权威文档, 这是GCC手册 , 这是GNU Make手册
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.