你在看错误的规则。 Download.c
实际上编译很好,但是链接阶段是错误的。
$ 制作
gcc -c -std=c99 Download.c #编译
gcc Download.c -o Program #链接
修复链接程序的 make 规则。它应该看起来像这样:
Program: a.o b.o c.o
$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
当你使用它时,我建议一个更完整的 Makefile 看起来像这样:
all: Program
clean:
rm -f Program *.o
.PHONY: all clean
# -c is implicit, you don't need it (it *shouldn't* be there)
# CC is also implicit, you don't need it
CFLAGS := -std=c99 -g -Wall -Wextra
Program: a.o b.o c.o
$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
# Make will automatically generate the necessary commands
# You just need to name which headers each file depends on
# (You can make the dependencies automatic but this is simpler)
a.o: a.c header.h
b.o: b.c header.h header2.h
c.o: c.c header.h
如何做错的例子
链接器标志实际上相当敏感!一定要按照我写的完全正确地输入上面的行,不要假设你写的内容是等价的。以下是一些错误且不应使用的命令略有不同的示例:
# WRONG: program must depend on *.o files, NOT *.c files
Program: a.c b.c c.c
$(CC) ...
# WRONG: -c should not be in CFLAGS
CFLAGS := -c -std=c99
Program: a.o b.o c.o
# WRONG: $(CFLAGS) should not be here
# you are NOT compiling, so they do not belong here
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
# WRONG: $(LIBS) MUST come at the end
# otherwise linker may fail to find symbols
$(CC) $(LDFLAGS) -o $@ $(LIBS) $^
# WRONG: do not list *.o files, use $^ instead
# otherwise it is easy to get typos here
$(CC) $(LDFLAGS) -o $@ a.o b.o c.o $(LIBS)
# WRONG: $(LDFLAGS) must be at the beginning
# it only applies to what comes after, so you
# MUST put it at the beginning
$(CC) -o $@ $(LDFLAGS) $^ $(LIBS)
# WRONG: -c flag disables linking
# but we are trying to link!
$(CC) $(LDFLAGS) -c -o $@ $^ $(LIBS)
# WRONG: use $(CC), not gcc
# Don't sabotage your ability to "make CC=clang" or "make CC=gcc-4.7"
gcc $(LDFLAGS) -o $@ $^ $(LIBS)
# WRONG: ld does not include libc by default!
ld $(LDFLAGS) -o $@ $^ $(LIBS)