Click here to Skip to main content
15,887,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
list.h:
C++
class list {
  private:
  int mCount = 5;

  public:
  int getCount();
};


core_list.cpp:
C++
#include "list.h"

int list::getCount() {
  return mCount;
}


main.cpp:
C++
#include "list.h"
#include <iostream>
int main() {
  list newList;
  std::cout << newList.getCount();
  getchar();
  return 0;
}


Makefile:
default:
  g++ -c *.cpp -O2 -Wall
  g++ -o Game.exe *.o
  rm -f *.o *~


Gives me:
main.o:main.cpp:(.text.startup+0x6d): undefined reference to `list::getCount()'
collect2.exe: error: ld returned 1 exit status
Makefile:2: recipe for target 'default' failed
mingw32-make: *** [default] Error 1


But if I modify the Makefile:
<pre lang="text">
default:
  g++ -c *.cpp -O2 -Wall
  #g++ -o Game.exe *.o
  rm -f *.o *~


So it only compiles and doesn't link, it compiles. It just won't link
for whatever reason. Thank you for the help.

What I have tried:

I have tried renaming the .cpp file to the same as its respective .h file, but to no avail. I have also modified the Makefile and noticed that it does compile, it just won't link.
Posted
Updated 22-Nov-22 8:16am

As an experiment, take the implementation of list::getCount() and put it in your main.cpp file. You should see it compile and link correctly. If so, that means the linker is not seeing the object file from core_list.cpp.

The reason it does not link when you comment that line out in the Makefile is because that is the line that does the linking. If anything, you should comment out the next line that removes the object files. That will let you see if there is an object file generated for core_list.cpp.
 
Share this answer
 
Your source codes and makefile work fine on my Windows 11 system (MinGW32-Make 3.82.90, g++ 10.3.0).

I suggest you to try, via the command line:
g++ -c *.cpp -O2 -Wall

and then
g++ -o Game.exe *.o

If the last command line gives link errors, then try
g++ -o Game.exe main.o list.o
 
Share this answer
 
v2
Check what .cpp files you have in your directory. You may be linking in something you're not expecting when you're using wildcards. Better yet, use the power of make:
make
CXXFLAGS = -Wall -Wextra -O2

Game.exe: core_list.o main.o
       $(CXX) $^ -o $@

clean:
       rm Game.exe *.o *~
Now make knows that Game.exe depends on list.o and main.o, and knows how to create them without you adding specific rules. Better, if you modify list.cpp, make will only compile that source file, and then link using the previously compiled main.o, which for large projects can be a huge time saver. Even better, you could tell make that the .o files depend on list.h so they should be recompiled if you modify core_list.h
make
CXXFLAGS = -Wall -Wextra -O2
core_list.o:  core_list.cpp list.h
main.o: main.cpp list.h

Game.exe: core_list.o main.o
      $(CXX) $^ -o $@

clean:
      rm Game.exe *.o *~
 
Share this answer
 
Comments
CodeSie Programming 22-Nov-22 14:36pm    
So the problem was that the list was a template class. I read that i have to define the functions in the header file, so i did. But it still gives me the error.
CodeSie Programming 22-Nov-22 14:36pm    
Also how do i delete a question? I want to post another similar question but not clutter up the questions thingy
k5054 22-Nov-22 15:27pm    
You might get some suggestions here https://stackoverflow.com/a/115735
As to deleting a question, I have no idea. It might be that you can't delete a question that already has solutions posted. And I'm not sure where you would go to ask ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900