Undefined reference to the "main" error in the crt1.o _start function

I have problems with my Makefile.

I am trying to create a program from 2 files - main.cpp, which contains the main function, and modules.c, which contains the definitions of the functions called in main (). modules.c contain only function definitions, there is no main function.

My makefile looks like this:

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 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 do this with this makefile, I get an error

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

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

I do not know what is wrong.

Thanks.

Regards, Rayne

+4
source share
2 answers

Here are some suggestions:

  • -o $@ $< not required for .o files, so remove them from these goals.
  • -Wall makes sense when used when compiling without binding. Therefore, I would add it to the CC and CXX (or better, to CFLAGS and CXXFLAGS ).
  • the net goal must be dependent on the .PHONY target, so you can always fulfill it (without first checking for changed dependencies).

If you still get a message about the lack of references to your functions from modules.c, you are probably missing some extern "C" ... statements in main.cpp. This is because the internal name of C ++ functions is calculated differently than from C functions (I think C ++ is the prefix of all names with namespace, class names, etc.). To tell C ++ that a specific function can be found using the old internal name for communication, use the extern expression "C".

+1
source

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

+8
source

Source: https://habr.com/ru/post/1347875/


All Articles