Why can I not link?

I'm putting together a small project to generate tetrahedralizations of 3D meshes:
https://github.com/blackears/cyclops_tetrahedralizer

This is the first time I've tried to use cmake to manage a project, and I'm getting a linking error and am not sure why. The error is:

1
2
3
main.obj : error LNK2019: unresolved external symbol "public: bool __cdecl CyclopsTetra3D::ObjFileLoader<float>::load_obj_file(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?load_obj_file@?$ObjFileLoade
r@M@CyclopsTetra3D@@QEAA_NAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function main [X:\dev\github.com\blackears\cyclopsTessellate\build\cyclopsTetrahedralizer.vcxproj]
X:\dev\github.com\blackears\cyclopsTessellate\build\Debug\cyclopsTetrahedralizer.exe : fatal error LNK1120: 1 unresolved externals [X:\dev\github.com\blackears\cyclopsTessellate\build\cyclopsTetrahedralizer.vcxproj]


It is resulting from
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using namespace CyclopsTetra3D;

...

int main(int argc, char **argv)
{
	std::map<std::string, std::string> options;
	std::string source_file;

	if (!parse_command_line(argc, argv, options, source_file))
	{
		return 1;
	}

	// Load the source file
	ObjFileLoader<float> loader;
	loader.load_obj_file(source_file);


The loader file it is referencing is
1
2
3
4
5
6
7
8
9
10
namespace CyclopsTetra3D {

template<typename T = float>
class ObjFileLoader
{

public:
    bool load_obj_file(const std::string& filename);

    ...


Which is defined in the .cpp file as

1
2
3
4
5
6
7
8
using namespace CyclopsTetra3D;

template<typename T>
bool ObjFileLoader<T>::load_obj_file(const std::string& filename) {
    points.clear();
    face_indices.clear();

    ...


The code looks correct to me. Maybe I've missed something in how to set up my CMakeLists.txt file? Or is the syntax wrong?
Last edited on
Template instantiations require the full definitions to be visible to the compiler, so there is little point to separating the header file from the implementation file.

Basically, you should collapse your obj_file_loader.cpp into your obj_file_loader.h file. Or #include "obj_file_loader.cpp" within the header file.

See also:
- https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file
- https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl
Registered users can post here. Sign in or register to post.