Makefiles and nested headers

My problem is with getting a makefile to run I'm new at this cause normally I just compile from the terminal or use my IDE.

So my problem is probaly with nested header dependencies. I have files that include header files that have header files that have header files. And all those header files have a .cpp file.

So I Googled the Internet to find a thread that said that if you have header files that include header files. You have to add those to the prequisites and that there is no way around this.

Basic Makefile and header dependencies

So I tried this but my case was a little different cause my nested headers also had .cpp files. So I tried adding those to the recipe place that didn't work. So I tried to add the .cpp files that were necessary to the prequisite location. That didn't work either.

  1 OBJECTS = main.o MovementHandle.o Cursor.o PromptHandle.o \
  2           Prompt.o NcursesOptions.o RT_Error.o Video.o Frame.o RGB.o
  3 
  4 CXXFLAGS = -std=c++17 -c
  5 LIBS = -lncurses
  6 
  7 drawterm : $(OBJECTS)
  8         g++ -o $(OBJECTS) $(LIBS)
  9 
 10 main.o : main.cpp MovementHandle.o PromptHandle.o
 11         g++ $(CXXFLAGS) main.cpp
 12 
 13 MovementHandle.o : MovementHandle.cpp MovementHandle.h Cursor.o
 14         g++ $(CXXFLAGS) MovementHandle.cpp
 15 
 16 Cursor.o : Cursor.cpp Cursor.h
 17         g++ $(CXXFLAGS) Cursor.cpp
 18 
 19 PromptHandle.o : PromptHandle.cpp PromptHandle.h Prompt.o Video.o
 20         g++ $(CXXFLAGS) PromptHandle.cpp
 21 
 22 Prompt.o : Prompt.cpp Prompt.h Cursor.o NcursesOptions.o
 23         g++ $(CXXFLAGS) Prompt.cpp
 24 
 25 NcursesOptions.o : NcursesOptions.cpp NcursesOptions.h RT_Error.o
 26         g++ $(CXXFLAGS) NcursesOptions.cpp
 27 
 28 RT_Error.o : RT_Error.cpp RT_Error.h
 29         g++ $(CXXFLAGS) RT_Error.cpp
 30 
 31 Video.o : Video.cpp Video.h Frame.o
 32         g++ $(CXXFLAGS) Video.cpp
 33 
 34 Frame.o : Frame.cpp Frame.h RGB.o
 35         g++ $(CXXFLAGS) Frame.cpp
 36 
 37 RGB.o : RGB.cpp RGB.h
 38         g++ $(CXXFLAGS) RGB.cpp

So I kept getting this and I know that it means:

/usr/bin/ld: /usr/lib/gcc/x86_64-pc-linux-gnu/8.3.0/../../../../lib/Scrt1.o: in function `_start':
    (.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [makefile:8: drawterm] Error 1

It means that make found a file that I haven't yet properly compiled meaning that make assumes it has a

int main()
{
    return 0;
}

In it but it isn't finding it because it isn't intended to be interpreted like that.

Upvotes: 0

Views: 516

Answers (1)

Zbigniew Zagórski
Zbigniew Zagórski

Reputation: 1981

I think that this is inferior line:

    g++ -o $(OBJECTS) $(LIBS)

When $(OBJECTS) is expanded it becomes (probably, because you didn't show declaration of OBJECTS variable):

    g++ -o main.o MovementHandle.o Cursor.o ...

-o option swallows main.o as it's argument, so this command means basically:

build program main.o from objects MovementHandle.o ...

Most probably you need to change it to something like this:

    g++ -o $@ $(OBJECTS)
    # $@ resolves to target, which in your case will be name of executable you want

Upvotes: 1

Related Questions