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
Use $^ instead of $<. The latter contains only the first dependency (modules.o), so main.o is not linked into the executable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With