use*_*819 15 c gcc compiler-errors makefile compilation
我有问题让我的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
Run Code Online (Sandbox Code Playgroud)
生成文件:
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
Run Code Online (Sandbox Code Playgroud)
Jen*_*ens 15
这个规则
main: producer.o consumer.o AddRemove.o
$(COMPILER) -pthread $(CCFLAGS) -o producer.o consumer.o AddRemove.o
Run Code Online (Sandbox Code Playgroud)
是错的.它说创建一个名为producer.o的文件,但是你想创建一个名为的文件-o producer.o.请原谅,但总是使用$ @参考目标:
main: producer.o consumer.o AddRemove.o
$(COMPILER) -pthread $(CCFLAGS) -o $@ producer.o consumer.o AddRemove.o
Run Code Online (Sandbox Code Playgroud)
这个错误意味着,在链接时,编译器无法在main()任何地方找到函数的定义.
在您的makefile中,main规则将扩展为类似的内容.
main: producer.o consumer.o AddRemove.o
gcc -pthread -Wall -o producer.o consumer.o AddRemove.o
Run Code Online (Sandbox Code Playgroud)
根据gcc 手册页,-o开关的使用如下
-o file 将输出放在文件文件中.无论是生成什么类型的输出,无论是可执行文件,目标文件,汇编程序文件还是预处理C代码,这都适用.如果
-o未指定,则默认为将可执行文件放入a.out.
这意味着,gcc会将输出放在-oswitch 旁边提供的文件名中.因此,这里不是将所有.o文件链接在一起并创建二进制文件[ main在您的情况下],而是创建二进制文件producer.o,链接其他.o文件.请更正.