Templates LNK2019 (in some projects)

Header (Lib.h, which derives from UObject):

template <typename Type>
static Type* CreateObject(FString Path);

CPP (Lib.cpp):

template <typename Type>
static Type* ULib::CreateObject(FString Path)
{
	return nullptr
}

Now this compiles. But when I call this in a constructor somewhere (with an include to Lib.h):

ULib::CreateObject<AActor>(""); // called from the constructor of ABaseSegment which derives from AActor.

error LNK2019: unresolved external
symbol “public: static class AActor *
__cdecl ULib::CreateObject(class FString)”
(??$CreateObject@VAActor@@@ULib@@SAPEAVAActor@@VFString@@@Z)
referenced in function “public:
__cdecl ABaseSegment::ABaseSegment(void)”
(??0ABaseSegment@@QEAA@XZ)

But… It works in my other projects… I tried removing the static-keyword but it didn’t help. I tried using a different type but nothing seems to work. Yes I added the line: include “Lib.h” to the project header file

// Below works but is not a template function.
static void foo(); // Lib.h
void ULib::foo(){} // Lib.cpp

So because the above works it means that the problem MUST BE with the template function. So I tried moving the template function from the Lib-class to the calling class. And yes that worked… But… why? Why does that work? Because this works I (for now) fixed the problem with an INLINE in the header file of Lib.h:

// Placed in Lib.h
template <typename Type>
static FORCEINLINE Type* CreateObject(FString Path)
{
	return nullptr;
}

The above code snippet works. I do not understand why. Most importantly the weird thing is that some functions require INLINE’s in the header while others do not… I do not understand how this is possible. I tried the exact same code in another project without the INLINE… And there it compiled without using the INLINE… I’m… Totally confused… Why does project A require an INLINE in the header while project B & C does not (all projects are UE4.13.1 and use the same VS version and the same PC)?

Hey -

What code exactly are you adding and to what file? I’m trying to setup a test project locally but I’m not sure where your template is being added. Are you using a class added through the editor? If so what is the parent of this class? If you are adding this class through Visual Studio and linking to your project, can you provide the full class code.

Hello,

Are you using a class added through the editor?
Yes I add all of my classes through the UE4 editor. Never manually.

If so what is the parent of this class?
UObject

If you are adding this class through Visual Studio and linking to your project, can you provide the full class code.

I do not add classes through VS. But here is a link to the full code for the library class and the constructor of a class derived from AActor that calls it: #pragma once#include "Object.h"#include "Lib.generated.h"/** * */ - Pastebin.com

Then I added this line to my project.h file to ensure it is included everywhere in my project:

#include "Lib.h"

If the function is not called at all then it will compile and run just fine. The moment I call that function however, I get the LNK2019 error. But only in one out of 3 projects I tested this in. That’s the weird thing and I can not figure out why it works in those other projects with the exact same Lib.h class (but with more functions of course).

And here is the workaround that works in all 3 projects using an INLINE and putting it all in the header and skipping the cpp entirely: #pragma once#include "Object.h"#include "Lib.generated.h"/** * */ - Pastebin.com I also removed the return nullptr; line and added the original code back in because that was just for debugging to ensure that the code inside the template was not causing the linker error.