【发布时间】:2013-12-29 03:46:42
【问题描述】:
我在让我的 makefile 正常工作时遇到问题。我遇到的第一个问题是对 main 的未定义引用。我的 producer.c 文件中有 main 作为函数。第二个问题是对 SearchCustomer() 的未定义引用。
错误:
bash-4.1$ make
gcc -Wall -c producer.c shared.h
gcc -Wall -c consumer.c shared.h
gcc -Wall -c AddRemove.c shared.h
gcc -pthread -Wall -o producer.o consumer.o AddRemove.o
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
AddRemove.o: In function `AddRemove':
AddRemove.c:(.text+0xb1): undefined reference to `SearchCustomer'
AddRemove.c:(.text+0x1e9): undefined reference to `SearchCustomer'
AddRemove.c:(.text+0x351): undefined reference to `SearchCustomer'
collect2: ld returned 1 exit status
make: *** [producer] Error 1
制作文件:
COMPILER = gcc
CCFLAGS = -Wall
all: main
debug:
make DEBUG=TRUE
main: producer.o consumer.o AddRemove.o
$(COMPILER) -pthread $(CCFLAGS) -o producer.o consumer.o AddRemove.o
producer.o: producer.c shared.h
$(COMPILER) $(CCFLAGS) -c producer.c shared.h
consumer.o: consumer.c shared.h
$(COMPILER) $(CCFLAGS) -c consumer.c shared.h
AddRemove.o: AddRemove.c shared.h
$(COMPILER) $(CCFLAGS) -c AddRemove.c shared.h
ifeq ($(DEBUG), TRUE)
CCFLAGS += -g
endif
clean:
rm -f *.o
【问题讨论】:
-
代码太长。缩小您的问题范围并提供Short, Self Contained, Correct (Compilable), Example。
-
一些建议:在
main配方中,您可以写:$(COMPILER) -o $@ $^ $(LDFLAGS)并将-pthread放入LDFLAGS。在其他规则中,您可以编写 once:%.o: %.c和配方:$(COMPILER) $(CCFLAGS) -c -o $@ $<(并且 not 将.h提供给编译器!)然后添加依赖项对于特殊目标,例如:producer.o: shared.h other_header.h和consumer.o: shared.h yet_another_header.h等。 -
其他几个建议:使用
$(MAKE)而不是仅仅make来保留调用的make 的命令行标志。您可能还想给--no-print-directory以防止几行无用的输出。此外,将CFLAGS用于C(而不是CCFLAGS,这可能会被误认为C++)。 -
以及更多:添加
.PHONY: all clean debug和其他实际上不是文件的目标,让make知道它不应该期望从配方中出现实际文件。您还可以使用@$(MAKE) DEBUG=TRUE(添加@)来防止make 回显命令。 -
最后,您可以从阅读手册中受益:gnu.org/software/make/manual/make.html
标签: c gcc compilation compiler-errors makefile