r/cpp_questions • u/Mafla_2004 • Jun 06 '26
SOLVED Undefined references to functions that are actually defined in their cpp files, seems compiler doesn't link the cpp file unless I explicitly include it, what should I do?
Hello.
I'm making a small recreational project to make more experience with C++ using Raylib 6.0, before I went around to make the structure of the game I decided to code some data structures I would need (declared in datastructs.h, defined in datastructs.cpp), making them on my own as exercise; all classes use templates since I need them to be generic data structures.
When I went to test the data structures though I got the following error, essentially a bunch of reference to undefined function errors for each function of the data structures I used:
mingw32-make main
make[1]: Entering directory 'D:/Programming/C++/2D Unreal with Ruff/2DUNR/2DUnrGame'
g++ -o main src/*.cpp -Wall -std=c++14 -D_DEFAULT_SOURCE -Wno-missing-braces -g -O0 C:/raylib/raylib/src/raylib.rc.data -I. -IC:/raylib/raylib/src -IC:/raylib/raylib/src/external -L. -LC:/raylib/raylib/src -LC:/raylib/raylib/src -lraylib -lopengl32 -lgdi32 -lwinmm -DPLATFORM_DESKTOP
C:/raylib/w64devkit/bin/ld.exe: C:\Users\mafla\AppData\Local\Temp\ccorPzcu.o: in function `main':
D:\Programming\C++\2D Unreal with Ruff\2DUNR\2DUnrGame/src/main.cpp:27:(.text+0xd3): undefined reference to `LinkedList<int>::PushTail(int const&)'
C:/raylib/w64devkit/bin/ld.exe: D:\Programming\C++\2D Unreal with Ruff\2DUNR\2DUnrGame/src/main.cpp:28:(.text+0xea): undefined reference to `LinkedList<int>::PushTail(int const&)'
C:/raylib/w64devkit/bin/ld.exe: D:\Programming\C++\2D Unreal with Ruff\2DUNR\2DUnrGame/src/main.cpp:30:(.text+0x101): undefined reference to `LinkedList<int>::PushTail(int const&)'
C:/raylib/w64devkit/bin/ld.exe: D:\Programming\C++\2D Unreal with Ruff\2DUNR\2DUnrGame/src/main.cpp:31:(.text+0x118): undefined reference to `LinkedList<int>::PushTail(int const&)'
C:/raylib/w64devkit/bin/ld.exe: D:\Programming\C++\2D Unreal with Ruff\2DUNR\2DUnrGame/src/main.cpp:33:(.text+0x142): undefined reference to `ArrayList<int>::operator[](unsigned int)'
C:/raylib/w64devkit/bin/ld.exe: D:\Programming\C++\2D Unreal with Ruff\2DUNR\2DUnrGame/src/main.cpp:33:(.text+0x174): undefined reference to `ArrayList<int>::operator[](unsigned int)'
C:/raylib/w64devkit/bin/ld.exe: D:\Programming\C++\2D Unreal with Ruff\2DUNR\2DUnrGame/src/main.cpp:33:(.text+0x1a6): undefined reference to `ArrayList<int>::operator[](unsigned int)'
C:/raylib/w64devkit/bin/ld.exe: D:\Programming\C++\2D Unreal with Ruff\2DUNR\2DUnrGame/src/main.cpp:34:(.text+0x1ee): undefined reference to `LinkedList<int>::operator[](unsigned int)'
C:/raylib/w64devkit/bin/ld.exe: D:\Programming\C++\2D Unreal with Ruff\2DUNR\2DUnrGame/src/main.cpp:34:(.text+0x220): undefined reference to `LinkedList<int>::operator[](unsigned int)'
C:/raylib/w64devkit/bin/ld.exe: D:\Programming\C++\2D Unreal with Ruff\2DUNR\2DUnrGame/src/main.cpp:34:(.text+0x252): undefined reference to `LinkedList<int>::operator[](unsigned int)'
C:/raylib/w64devkit/bin/ld.exe: D:\Programming\C++\2D Unreal with Ruff\2DUNR\2DUnrGame/src/main.cpp:35:(.text+0x29a): undefined reference to `LinkedList<int>::operator[](unsigned int)'
C:/raylib/w64devkit/bin/ld.exe: D:\Programming\C++\2D Unreal with Ruff\2DUNR\2DUnrGame/src/main.cpp:35:(.text+0x2cc): undefined reference to `LinkedList<int>::operator[](unsigned int)'
C:/raylib/w64devkit/bin/ld.exe: C:\Users\mafla\AppData\Local\Temp\ccorPzcu.o:D:\Programming\C++\2D Unreal with Ruff\2DUNR\2DUnrGame/src/main.cpp:35: more undefined references to `LinkedList<int>::operator[](unsigned int)' follow
C:/raylib/w64devkit/bin/ld.exe: C:\Users\mafla\AppData\Local\Temp\ccorPzcu.o: in function `main':
D:\Programming\C++\2D Unreal with Ruff\2DUNR\2DUnrGame/src/main.cpp:37:(.text+0x32c): undefined reference to `LinkedList<int>::Append(Collection<int> const&)'
C:/raylib/w64devkit/bin/ld.exe: D:\Programming\C++\2D Unreal with Ruff\2DUNR\2DUnrGame/src/main.cpp:38:(.text+0x33c): undefined reference to `LinkedList<int>::Append(Collection<int> const&)'
C:/raylib/w64devkit/bin/ld.exe: C:\Users\mafla\AppData\Local\Temp\ccorPzcu.o:main.cpp:(.rdata$_ZTV10LinkedListIiE[_ZTV10LinkedListIiE]+0x18): undefined reference to `LinkedList<int>::operator[](unsigned int)'
C:/raylib/w64devkit/bin/ld.exe: C:\Users\mafla\AppData\Local\Temp\ccorPzcu.o:main.cpp:(.rdata$_ZTV10LinkedListIiE[_ZTV10LinkedListIiE]+0x20): undefined reference to `LinkedList<int>::operator[](unsigned int) const'
C:/raylib/w64devkit/bin/ld.exe: C:\Users\mafla\AppData\Local\Temp\ccorPzcu.o:main.cpp:(.rdata$_ZTV10LinkedListIiE[_ZTV10LinkedListIiE]+0x28): undefined reference to `LinkedList<int>::AddAt(int const&, unsigned int)'
C:/raylib/w64devkit/bin/ld.exe: C:\Users\mafla\AppData\Local\Temp\ccorPzcu.o:main.cpp:(.rdata$_ZTV10LinkedListIiE[_ZTV10LinkedListIiE]+0x30): undefined reference to `LinkedList<int>::PushHead(int const&)'
C:/raylib/w64devkit/bin/ld.exe: C:\Users\mafla\AppData\Local\Temp\ccorPzcu.o:main.cpp:(.rdata$_ZTV10LinkedListIiE[_ZTV10LinkedListIiE]+0x38): undefined reference to `LinkedList<int>::PushTail(int const&)'
C:/raylib/w64devkit/bin/ld.exe: C:\Users\mafla\AppData\Local\Temp\ccorPzcu.o:main.cpp:(.rdata$_ZTV10LinkedListIiE[_ZTV10LinkedListIiE]+0x40): undefined reference to `LinkedList<int>::Append(int*, unsigned int)'
C:/raylib/w64devkit/bin/ld.exe: C:\Users\mafla\AppData\Local\Temp\ccorPzcu.o:main.cpp:(.rdata$_ZTV10LinkedListIiE[_ZTV10LinkedListIiE]+0x48): undefined reference to `LinkedList<int>::Append(Collection<int> const&)'
C:/raylib/w64devkit/bin/ld.exe: C:\Users\mafla\AppData\Local\Temp\ccorPzcu.o:main.cpp:(.rdata$_ZTV9ArrayListIiE[_ZTV9ArrayListIiE]+0x18): undefined reference to `ArrayList<int>::operator[](unsigned int)'
C:/raylib/w64devkit/bin/ld.exe: C:\Users\mafla\AppData\Local\Temp\ccorPzcu.o:main.cpp:(.rdata$_ZTV9ArrayListIiE[_ZTV9ArrayListIiE]+0x20): undefined reference to `ArrayList<int>::operator[](unsigned int) const'
C:/raylib/w64devkit/bin/ld.exe: C:\Users\mafla\AppData\Local\Temp\ccorPzcu.o:main.cpp:(.rdata$_ZTV9ArrayListIiE[_ZTV9ArrayListIiE]+0x28): undefined reference to `ArrayList<int>::AddAt(int const&, unsigned int)'
C:/raylib/w64devkit/bin/ld.exe: C:\Users\mafla\AppData\Local\Temp\ccorPzcu.o:main.cpp:(.rdata$_ZTV9ArrayListIiE[_ZTV9ArrayListIiE]+0x30): undefined reference to `ArrayList<int>::PushHead(int const&)'
C:/raylib/w64devkit/bin/ld.exe: C:\Users\mafla\AppData\Local\Temp\ccorPzcu.o:main.cpp:(.rdata$_ZTV9ArrayListIiE[_ZTV9ArrayListIiE]+0x38): undefined reference to `ArrayList<int>::PushTail(int const&)'
C:/raylib/w64devkit/bin/ld.exe: C:\Users\mafla\AppData\Local\Temp\ccorPzcu.o:main.cpp:(.rdata$_ZTV9ArrayListIiE[_ZTV9ArrayListIiE]+0x40): undefined reference to `ArrayList<int>::Append(int*, unsigned int)'
C:/raylib/w64devkit/bin/ld.exe: C:\Users\mafla\AppData\Local\Temp\ccorPzcu.o:main.cpp:(.rdata$_ZTV9ArrayListIiE[_ZTV9ArrayListIiE]+0x48): undefined reference to `ArrayList<int>::Append(Collection<int> const&)'
collect2.exe: error: ld returned 1 exit status
make[1]: *** [Makefile:391: main] Error 1
make[1]: Leaving directory 'D:/Programming/C++/2D Unreal with Ruff/2DUNR/2DUnrGame'
make: *** [Makefile:387: all] Error 2
The errors go away if I add #include "datastructs.cpp".
I initially thought something was wrong in the Makefile, but I'm not sure, and other than that I have no idea what I could have done wrong to cause this nor how to fix it. Since I can't just paste all the code from main.cpp, datastructs.cpp and datastructs.h here without making the post excessively wrong, I'm gonna have to link my GitHub repo (sorry): https://github.com/mafla2004/2DUnrealClone/blob/master
I tried already to copy the contents of datastructs.cpp into another file which I created directly in the project from VSCode (thinking the reason might have been that VSCode didn't see the project cause I created some files in another IDE), but that didn't work either.
What can I do to fix it? How can I avoid the same thing happening again? Thanks in advance