Download

Link error when using world context object and engine utils

So, here’s the code:

.h

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "UIUtilityLibrary.generated.h"

UCLASS()
class WIDGETMODULE_API UUIUtilityLibrary : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
public:
	UFUNCTION(BlueprintCallable, Category = "UI Utility Library", meta = (WorldContext="WorldContextObject"))
	static class AWidgetManager* GetGlobalWidgetManager(class UObject* WorldContextObject);
	
};

.cpp

#include "Utility/UIUtilityLibrary.h"
#include "UI/Manager/WidgetManager.h"
#include "EngineUtils.h"


AWidgetManager* GetGlobalWidgetManager(UObject* WorldContextObject)
{
    if (GEngine == nullptr) return nullptr;

    UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
    if (World != nullptr)
    {
        for (TActorIterator<AWidgetManager> Iterator(World, AWidgetManager::StaticClass()); Iterator; ++Iterator)
        {
            AWidgetManager* Manager = *Iterator;
            return Manager;
        }
    }

    return nullptr;
}

This, somehow, yields a linking error in the .gen.obj class:

CompilerResultsLog: Error:   UIUtilityLibrary.gen.cpp.obj : error LNK2019: unresolved external symbol "public: static class AWidgetManager * __cdecl UUIUtilityLibrary::GetGlobalWidgetManager(class UObject *)" (?GetGlobalWidgetManager@UUIUtilityLibrary@@SAPEAVAWidgetManager@@PEAVUObject@@@Z) referenced in function "public: static void __cdecl UUIUtilityLibrar
y::execGetGlobalWidgetManager(class UObject *,struct FFrame &,void * const)" (?execGetGlobalWidgetManager@UUIUtilityLibrary@@SAXPEAVUObject@@AEAUFFrame@@QEAX@Z)

and I have absolutely no clue why. Usually these are linked to something not being included in the build.cs files. Mine are:

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "AIModule" });
PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });

What am I missing? Why is this linker error happening?

P.S. I did the usual delete Intermediate and Binaries folder, but this did not change anything.

Sigh. Every day is a struggle.

.cpp file

AWidgetManager* GetGlobalWidgetManager(UObject* WorldContextObject)
should be
AWidgetManager* UUIUtilityLibrary::GetGlobalWidgetManager(UObject* WorldContextObject)

This is what happens when you manually create function definitions.

1 Like