简体   繁体   中英

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:

 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

No you're not. The make command:

C:\Users\PATTY\Desktop\loops>mingw32-make justify

doesn't specify any 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. But the directory listing of C:\\Users\\PATTY\\Desktop\\loops shows that neither of those makefiles exists either.

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.

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 .

This rule matches for target justify and source file justify.c . And that source file exists in the working directory, so make tries the recipe:

    $(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@

with prerequisite $^ = justify.c and target $@ = 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. That is because $(LINK.c) is defined:

LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)

You have no such program as cc in your PATH , so the recipe fails:

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. 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 . 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. You can save this makefile in C:\\Users\\PATTY\\Desktop\\loops as either makefile or Makefile and make will use it by default. Or you can call it whatever you like, and specify it by name with the -f option when you invoke make :

mingw32-make -f whatever.mak ...

Here is a fairly sound beginner's tutorial for using GCC with GNU make . For authoritative documentation, here is the GCC manual and here is the GNU Make manual

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM