Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference to 'main' error in crt1.o function _start

Tags:

c++

c

makefile

I've having problems with my Makefile.

I'm trying to create a program from 2 files - main.cpp that contains the main function, and modules.c that contains the definitions of the functions that are called in main(). modules.c only contain function definitions, no main function.

My Makefile is as follows:

CC := gcc
CXX := g++
LINK := g++ -Wall
CFLAGS := -g
CXXFLAGS := -g

TARGET = program

$(TARGET): modules.o main.o
   $(LINK) -o $@ $< -lpcap

clean:
   rm *.o $(TARGET)

modules.o:
   $(CC) $(CFLAGS) -c modules.c -o $@ $<

main.o:
   $(CXX) $(CXXFLAGS) -c main.cpp -o $@ $<

I have included "modules.h", which contains all the function declarations, in my main.cpp. The CFLAGS and CXXFLAGS variables point to the correct paths containing

When I try to make using this Makefile, I get the error

/usr/lib/gcc/x86_64-redhat-linux/4.4.4/../../../../lib64/crt1.o: In function '_start':
(.text+0x20): undefined reference to 'main'

If I switch the order of modules.o and main.o in my $(TARGET) line, then I get errors that say "undefined reference to" the functions I have defined in modules.c, in main.cpp.

I don't know what is wrong.

Thank you.

Regards, Rayne

like image 491
Rayne Avatar asked Dec 27 '22 22:12

Rayne


1 Answers

Use $^ instead of $<. The latter contains only the first dependency (modules.o), so main.o is not linked into the executable.

like image 51
Patrick Georgi Avatar answered Jan 14 '23 11:01

Patrick Georgi