Warm tip: This article is reproduced from serverfault.com, please click

c-(.text + 0x20):对“ main”的未定义引用和对函数的未定义引用

(c - (.text+0x20): undefined reference to `main' and undefined reference to function)

发布于 2013-12-11 08:53:14

我在使我的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
Questioner
user2644819
Viewed
0
Jens 2019-12-02 16:48:48

这条规则

main: producer.o consumer.o AddRemove.o
   $(COMPILER) -pthread $(CCFLAGS) -o producer.o consumer.o AddRemove.o

是错的。它说要创建一个名为producer.o(带有-o producer.o的文件,但是你要创建一个名为的文件main请原谅,但总是使用$ @来引用目标

main: producer.o consumer.o AddRemove.o
   $(COMPILER) -pthread $(CCFLAGS) -o $@ producer.o consumer.o AddRemove.o

正如Shahbaz正确指出的那样,gmake专业人员还将使用$^扩展到规则中所有先决条件的工具。通常,如果你发现自己重复了一个字符串或名称,那说明你做错了,应该使用一个变量,无论是内置变量还是创建变量。

main: producer.o consumer.o AddRemove.o
   $(COMPILER) -pthread $(CCFLAGS) -o $@ $^