2

我有这个hello-world.c我想编译成hello-world二进制。但hello-world.c取决于 and 中定义的一些函数../helpers/a.c../helpers/b.c并且每个助手都分别包括../helpers/a.hand ../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,但我不明白为什么。

编辑:生成文件调试:

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
CFLAGS  +=  

LDLIBS  =   
LDLIBS  +=  

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

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

new: clean all

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

all: $(DESTS)

hello-world: $(OBJS)
    $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)

其中一个问题是我写的一个糟糕的清理规则已经擦除了 ac 和 bc 并且 Makefile 正因此而起作用。

于 2013-09-30T08:27:30.367 回答
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 回答