2

バイナリhello-world.cにコンパイルしたいこれがあります。hello-worldただし、およびでhello-world.c定義されたいくつかの関数に依存し、これらのヘルパーにはそれぞれおよびが含まれます。../helpers/a.c../helpers/b.c../helpers/a.h../helpers/b.h

私の現在のMakefileは次のようになります

CC      =   @gcc
CFLAGS  =   -g -Wall -Wextra -Werror
CFLAGS  +=  

LDLIBS  =   
LDLIBS  +=  

OBJS    =   ../helpers/a.o ../helpers/b.o

SOURCES =   hello-world.c
DESTS   =   hello-world

new: clean all

clean:
    @rm -rf *.o */*.o $(DESTS)

all: $(OBJS) $(DESTS)

%.o: %.c
    $(CC) $(CFLAGS) -c $< -o $@

%: %.c
    $(CC) $(CFLAGS) -o $@ $<

しかし、それは機能せず、戻りますmake: *** No rule to make target `../helpers/a.o', needed by `all'. Stop.

Makefile が のルールを認識していないように見えることは理解していますが%.o、その理由はわかりません。

編集: Makefile デバッグ:

alexandernst@stupidbox:/media/sf_procmon/procmon$ make --debug=b
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

This program built for x86_64-pc-linux-gnu
Reading makefiles...
Updating goal targets....
 File `new' does not exist.
   File `clean' does not exist.
  Must remake target `clean'.
  Successfully remade target file `clean'.
   File `all' does not exist.
     File `../helpers/a.o' does not exist.
    Must remake target `../helpers/a.o'.
make: *** No rule to make target `../helpers/a.o', needed by `all'.  Stop.
4

2 に答える 2

0

Makefile を親ディレクトリに置くと、作業がはるかに簡単になります。次に、次のように書くことができます。

CC        = gcc
CFLAGS    = -g -Wall -Wextra -Werror
CPPFLAGS  = -Ihelpers

DESTS       = hello-world/hello-world
OBJS        = helpers/a.o helpers/b.o hello-world/hello-world.o

# Do not make 'all' depend directly on object files.
all: $(DESTS)

# Clean rules should always be prefixed with '-' to avoid
# errors when files do not exist.  Do not use 'rm -r' when
# there shouldn't be directories to delete; do not delete
# wildcards when you can use explicit lists instead.
# Never use '@'.
clean:
        -rm -f $(OBJS) $(DESTS)

# An explicit linkage rule is needed for each entry in DESTS.
hello-world/hello-world: hello-world/hello-world.o \
                         helpers/a.o helpers/b.o
        $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)

# The main function of these dependency lists is to prevent
# Make from deleting object files after each build.  We also
# take the opportunity to specify header dependencies.
# We rely on the built-in %.o:%.c rule for commands.
hello-world/hello-world.o: hello-world/hello-world.c \
                           helpers/a.h helpers/b.h
helpers/a.o: helpers/a.c helpers/a.h
helpers/b.o: helpers/b.c helpers/b.h

# This tells Make that 'all' and 'clean' are not files to be created.
.PHONY: all clean

この手法の詳細については、流域論文「Recursive Make Considered Harmful」および関連する実装ノートを参照してください。

于 2013-09-29T14:15:31.013 に答える