0

EDIT: An answer has been found, and it was a simple mistake. I do not feel that this is a duplicate because I know what the error indicates (that the main function was unable to find a reference to the object class), I just thought that I had included it properly and needed another set of eyes to point out the error in my makefile line.

I apologize in advance if I use the wrong terminology, or I am misunderstanding something simple. I am slightly familiar with C and C# and this is my attempt at trying C++. There are other issues in the code, such as naming inconsistencies but please ignore these as they are not the issue at hand. I am also in the process of restructuring/redocumenting the project so please excuse the mess.

What I Have

I've been writing a small C++ application using vim and been compiling it using g++. It worked up until a point. The error didn't make sense to me, so I decided to try Clion in an attempt to debug it but, much to my surprise, it built and ran as expected.

From the terminal, I move into the project root directory (one level above src) and run make:

g++ -c src/item.cpp -o obj/item.o
g++ -c src/hero.cpp -o obj/hero.o
g++ -c src/main.cpp -o obj/main.o
g++ -o oChre.exe obj/hero.o obj/main.o -lm
obj/main.o: In function `main':
main.cpp:(.text+0x467): undefined reference to Hand::Hand(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
main.cpp:(.text+0x49e): undefined reference to `Item::getName[abi:cxx11]()'
collect2: error: ld returned 1 exit status
makefile:7: recipe for target 'program' failed
make: *** [program] Error 1

From inside Clion, I just click Build and Run and the project builds and runs exactly how I would expect it to.

What I Suspect Is The Issue

My makefile is as follows:

CC=g++
EXT=o
PURGE=rm

#The main bin
program: item.o hero.$(EXT) main.$(EXT)
    $(CC) -o oChre.exe obj/hero.$(EXT) obj/main.$(EXT) -lm
main.o:
    $(CC) -c src/main.cpp -o obj/main.o

#Creatures
hero.o:
    $(CC) -c src/hero.cpp -o obj/hero.o

#items
item.o:
    $(CC) -c src/item.cpp -o obj/item.o

clean:
    $(PURGE) obj/*.$(EXT) *.exe

I suspect Clion isn't using my makefile as IDE's are typically capable of determining what to compile and how on their own. This is why I think Clion is successfully running.

What Will Solve My Problem

I would like if someone could answer one or both of the following questions:

  1. Why does g++ not properly compile? Is there something wrong with my makefile or is my compiler improperly configured? I expect this project to expand in the future, so will it be worth maintaining my own makefile or should I just leave that up to an IDE.
  2. Why does Clion compile my code? Can I just get the makefile it generates and put it in my repo so others can compile it?

If I am missing something important, please ask or I have all the code on GitHub.

Nathan Smith
  • 683
  • 1
  • 10
  • 24
  • 1
    Writing make files first by hand is a good way to learn to understand how compilation works. Which can be helpful when debugging problems when building with an IDE/cmake. When projects get large CMake or an IDE will save you a lot of work maintaining dependencies in your makefile. CMake is quite popular for cross platform code previously the autotools where used most but these are very hard to learn. – Eelke Sep 05 '17 at 05:52
  • Possible duplicate: [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – n. m. could be an AI Sep 05 '17 at 05:55
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Tsyvarev Sep 05 '17 at 07:15

1 Answers1

2

Your link command is missing item.o

#The main bin
program: item.o hero.$(EXT) main.$(EXT)
    $(CC) -o oChre.exe obj/hero.$(EXT) obj/main.$(EXT) -lm

should be

#The main bin
program: item.o hero.$(EXT) main.$(EXT)
    $(CC) -o oChre.exe obj/item.o obj/hero.$(EXT) obj/main.$(EXT) -lm
Eelke
  • 20,897
  • 4
  • 50
  • 76