"Undefined reference to main" 与 main 分开 script/rule

"Undefined reference to main" with main in a seperate script/rule

这可能是 makefile error: undefined reference to main or Undefined reference in main Makefile or a few others. Both crc64 and getWord are supporting files for mainProg, which contains my main function. When I try to run my make file I am getting the compilation error below regarding my rules for crc64.o. In the c file I have the include statements and header files laid out in this post Creating your own header file in C 的重复,因此我不应该出现与将 header 链接到 body 相关的链接错误。

错误: gcc -g -Wall -std=c99 crc64.o -o crc64 /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function ``_start': (.text+0x20): undefined reference to ``main' collect2: error: ld returned 1 exit status
生成文件

CC=gcc
COPTS=-g -Wall -std=c99
ALL=crc64 getWord mainProg
all: $(ALL)

crc64: crc64.o
   $(CC) $(COPTS) $^ -o $@
getWord: getWord.o
   $(CC) $(COPTS) $^ -o $@
mainProg: getWord.o crc64.o mainProg.o 
   $(CC) $(COPTS) $^ -o $@
crc64.o: crc64.c crc64.h
getWord.o: getWord.c getWord.h
mainProg.o: mainProg.c getWord.h crc64.h
.c.o:
   $(CC) -c $(COPTS) $<

您正在编译 crc64getWord,就像它们是可执行文件一样。因此,他们需要一个 main 函数。

只需删除这两个目标即可。你不需要它们。

另请参阅@mafso 的评论:您应该使用 COPTSCFLAGS instrad,以确保相关的 implicit rules 选择相同的选项。

The following makefile would (probably) be just what your project needs
note: be sure to replace '<tab>' with the tab character


CC=gcc

COPTS := -g -Wall -Wextra -Wpedantic -std=c99

LOPTS := -g

TARGET := mainProg

SRCS := $(wildcard:*.c)
OBJS := $(SRCS:.c=.o)
HDRS := $(SRCS:.c=.h)

.PHONY: all

all: ${TARGET} 

${TARGET}: $(OBJS)
<tab>$(CC) $(COPTS) $^ -o $@

.c.o: #{HDRS}
<tab>$(CC) -c $(COPTS) -o $^ $<