0

This is a possible dup of 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 so I should not be having linking errors related to linking header to the body.

ERROR: 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
Makefile

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) $<
Community
  • 1
  • 1
CodeMonkey
  • 268
  • 5
  • 16
  • in the makefile, use ':=' when defining a macro rather than '=' because when using '=' the macro will be re-evaluated each time it is invoked while the ':=' will only be evaluated once. – user3629249 Jan 14 '15 at 23:38

2 Answers2

7

You are compiling crc64 and getWord as if they were executables. As such, they need a main function.

Simply remove those two targets. You don't need them.

Also see @mafso's comment: you should use CFLAGS instrad of COPTS, to make sure the relevant implicit rules pick up the same options.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
0
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 $^ $<
user3629249
  • 16,402
  • 1
  • 16
  • 17