0

I have 4 files world.cpp, world.hpp, Helpers/tools.cpp, Helpers/tools.hpp.

wrold.cpp and tools.cpp include their respective headers and world.hpp includes tools.hpp

world.hpp/cpp use structures defined in tools.hpp/cpp

on my CMake file I have added

target_sources(voxel-world PUBLIC ${PROJECT_SOURCE_DIR}/Helpers/tools.cpp)

and

target_sources(voxel-world PUBLIC ${PROJECT_SOURCE_DIR}/World.cpp)

So, to my understading, the generated make file should attempt to link everything properly.

However when I attempt to compile my program I get the error message:

    CMakeFiles/voxel-world.dir/source/World.cpp.o: In function `Chunk_Holder::Chunk_Holder()':
    World.cpp:(.text+0x112a): undefined reference to `cirArray<cirArray<cirArray<Chunk*> > >::cirArray()'
    CMakeFiles/voxel-world.dir/source/World.cpp.o: In function `Chunk_Holder::Chunk_Holder(int, int, int, World*)':
    World.cpp:(.text+0x117c): undefined reference to `cirArray<cirArray<cirArray<Chunk*> > >::cirArray()'
    World.cpp:(.text+0x11a2): undefined reference to `cirArray<cirArray<cirArray<Chunk*> > >::cirArray(unsigned int)'
    World.cpp:(.text+0x11ee): undefined reference to `cirArray<cirArray<Chunk*> >::cirArray(unsigned int)'
 ...
 ...

I am not sure why the linker is failing to find these declarations, since the include statements are on each file. What have I missed?

Makogan
  • 8,208
  • 7
  • 44
  • 112

1 Answers1

0

The problem was caused because cirArray is a templated class, and apparently the compiler is unable to generate the relevant code if the implementation is in the .cpp file.

The solution is thus to move the class implementation into the header.

Makogan
  • 8,208
  • 7
  • 44
  • 112