unresolved external symbol when using UUserWidget

Hello im trying to create a widget inside a slate but im having some troubles.

This is my cpp
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.

#include "teste.h"
#include "testeStyle.h"
#include "testeCommands.h"
#include "LevelEditor.h"
#include "Editor.h"
#include "Runtime/UMG/Public/Components/Widget.h"
#include "Widgets/Docking/SDockTab.h"
#include "Widgets/Layout/SBox.h"
#include "Widgets/Text/STextBlock.h"
#include "Framework/MultiBox/MultiBoxBuilder.h"

static const FName testeTabName("teste");

#define LOCTEXT_NAMESPACE "FtesteModule"

void FtesteModule::StartupModule()
{
	// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module
	
	FtesteStyle::Initialize();
	FtesteStyle::ReloadTextures();

	FtesteCommands::Register();
	
	PluginCommands = MakeShareable(new FUICommandList);

	PluginCommands->MapAction(
		FtesteCommands::Get().OpenPluginWindow,
		FExecuteAction::CreateRaw(this, &FtesteModule::PluginButtonClicked),
		FCanExecuteAction());
		
	FLevelEditorModule& LevelEditorModule = FModuleManager::LoadModuleChecked<FLevelEditorModule>("LevelEditor");
	
	{
		TSharedPtr<FExtender> MenuExtender = MakeShareable(new FExtender());
		MenuExtender->AddMenuExtension("WindowLayout", EExtensionHook::After, PluginCommands, FMenuExtensionDelegate::CreateRaw(this, &FtesteModule::AddMenuExtension));

		LevelEditorModule.GetMenuExtensibilityManager()->AddExtender(MenuExtender);
	}
	
	{
		TSharedPtr<FExtender> ToolbarExtender = MakeShareable(new FExtender);
		ToolbarExtender->AddToolBarExtension("Settings", EExtensionHook::After, PluginCommands, FToolBarExtensionDelegate::CreateRaw(this, &FtesteModule::AddToolbarExtension));
		
		LevelEditorModule.GetToolBarExtensibilityManager()->AddExtender(ToolbarExtender);
	}
	
	FGlobalTabmanager::Get()->RegisterNomadTabSpawner(testeTabName, FOnSpawnTab::CreateRaw(this, &FtesteModule::OnSpawnPluginTab))
		.SetDisplayName(LOCTEXT("FtesteTabTitle", "teste"))
		.SetMenuType(ETabSpawnerMenuType::Hidden);
}

void FtesteModule::ShutdownModule()
{
	// This function may be called during shutdown to clean up your module.  For modules that support dynamic reloading,
	// we call this function before unloading the module.
	FtesteStyle::Shutdown();

	FtesteCommands::Unregister();

	FGlobalTabmanager::Get()->UnregisterNomadTabSpawner(testeTabName);
}

UObject* FtesteModule::LoadAssetFromContent(FString Path, FString Name)
{

	FString FullPath = Path + Name;
	FStringAssetReference* assetRef = new FStringAssetReference(FullPath);
	FStreamableManager assetLoader;
	UObject* loadedAsset = nullptr;

	loadedAsset = assetLoader.LoadSynchronous(*assetRef,true);
	delete assetRef;

	return loadedAsset;

}

TSharedRef<SDockTab> FtesteModule::OnSpawnPluginTab(const FSpawnTabArgs& SpawnTabArgs)
{
	UBlueprint* loadedWidget = Cast<UBlueprint>(LoadAssetFromContent("/Content/Widgets", "MyUMG"));

	if (loadedWidget)
	{

		UClass* tempWidget = loadedWidget->GeneratedClass;
		createdWidget = CreateWidget<UUserWidget>(GEditor->GetEditorWorldContext().World(), tempWidget);
		tempWidget = nullptr;

	}

	FText WidgetText = FText::Format(
		LOCTEXT("WindowWidgetText", "Add code to {0} in {1} to override this window's contents"),
		FText::FromString(TEXT("FtesteModule::OnSpawnPluginTab")),
		FText::FromString(TEXT("teste.cpp"))
		);

	return SNew(SDockTab)
		.TabRole(ETabRole::NomadTab)
		[
			// Put your tab content here!
			SNew(SBox)
			.HAlign(HAlign_Center)
			.VAlign(VAlign_Center)
			[
				createdWidget->TakeWidget()
			]
		];
}

void FtesteModule::PluginButtonClicked()
{
	FGlobalTabmanager::Get()->InvokeTab(testeTabName);
}

void FtesteModule::AddMenuExtension(FMenuBuilder& Builder)
{
	Builder.AddMenuEntry(FtesteCommands::Get().OpenPluginWindow);
}

void FtesteModule::AddToolbarExtension(FToolBarBuilder& Builder)
{
	Builder.AddToolBarButton(FtesteCommands::Get().OpenPluginWindow);
}

#undef LOCTEXT_NAMESPACE
	
IMPLEMENT_MODULE(FtesteModule, teste)

and this is my .h
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.

#pragma once
#include "Engine.h"
#include "Runtime/Engine/Classes/Engine/StreamableManager.h"
#include "Runtime/UMG/Public/UMG.h"
#include "Runtime/UMG/Public/UMGStyle.h"
#include "Runtime/UMG/Public/Slate/SObjectWidget.h"
#include "Runtime/UMG/Public/IUMGModule.h"
#include "Runtime/UMG/Public/Blueprint/UserWidget.h"
#include "CoreMinimal.h"
#include "ModuleManager.h"

class FToolBarBuilder;
class FMenuBuilder;

class FtesteModule : public IModuleInterface
{
public:

	/** IModuleInterface implementation */
	virtual void StartupModule() override;
	virtual void ShutdownModule() override;
	
	/** This function will be bound to Command (by default it will bring up plugin window) */
	void PluginButtonClicked();
	UObject* LoadAssetFromContent(FString Path, FString Name);

	UPROPERTY()
	UUserWidget* createdWidget = nullptr;
	
private:

	void AddToolbarExtension(FToolBarBuilder& Builder);
	void AddMenuExtension(FMenuBuilder& Builder);

	TSharedRef<class SDockTab> OnSpawnPluginTab(const class FSpawnTabArgs& SpawnTabArgs);

private:
	TSharedPtr<class FUICommandList> PluginCommands;
};

This is my error:

    Severity	Code	Description	Project	File	Line	Suppression State
    Error	LNK2019	unresolved external symbol "__declspec(dllimport) public: class TSharedRef<class SWidget,0> __cdecl UWidget::TakeWidget(void)" (__imp_?TakeWidget@UWidget@@QEAA?AV?$TSharedRef@VSWidget@@$0A@@@XZ) referenced in function "private: class TSharedRef<class SDockTab,0> __cdecl FtesteModule::OnSpawnPluginTab(class FSpawnTabArgs const &)" (?OnSpawnPluginTab@FtesteModule@@AEAA?AV?$TSharedRef@VSDockTab@@$0A@@@AEBVFSpawnTabArgs@@@Z)	BuildingEscape	C:\repos\03_building_escape\BuildingEscape\Intermediate\ProjectFiles\Module.teste.cpp.obj	1	
    
    
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	LNK2019	unresolved external symbol "__declspec(dllimport) public: static class UClass * __cdecl UUserWidget::StaticClass(void)" (__imp_?StaticClass@UUserWidget@@SAPEAVUClass@@XZ) referenced in function "class UUserWidget * __cdecl CreateWidget<class UUserWidget>(class UWorld *,class UClass *)" (??$CreateWidget@VUUserWidget@@@@YAPEAVUUserWidget@@PEAVUWorld@@PEAVUClass@@@Z)	BuildingEscape	C:\repos\03_building_escape\BuildingEscape\Intermediate\ProjectFiles\Module.teste.cpp.obj	1	
    
    
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	LNK2019	unresolved external symbol "__declspec(dllimport) public: static class UUserWidget * __cdecl UUserWidget::CreateWidgetOfClass(class UClass *,class UGameInstance *,class UWorld *,class APlayerController *)" (__imp_?CreateWidgetOfClass@UUserWidget@@SAPEAV1@PEAVUClass@@PEAVUGameInstance@@PEAVUWorld@@PEAVAPlayerController@@@Z) referenced in function "class UUserWidget * __cdecl CreateWidget<class UUserWidget>(class UWorld *,class UClass *)" (??$CreateWidget@VUUserWidget@@@@YAPEAVUUserWidget@@PEAVUWorld@@PEAVUClass@@@Z)	BuildingEscape	C:\repos\03_building_escape\BuildingEscape\Intermediate\ProjectFiles\Module.teste.cpp.obj	1	

I dont know how to fix this. I also added UMG to my Modules…
Thanks for your time!

Have you tried this?

#include "Blueprint/UserWidget.h"

Its generally the only thing I include. Hope it helps! Make it a great day!

Yesh i did try, but no luck! Thank you!

You linker error actually goes thru to UMG module it self as it can not resolve links that are referenced in UMG module. I don’t know if this gonna fix the issue but i notice other issue in your code, this:

 UPROPERTY()
 UUserWidget* createdWidget = nullptr;

UPROPERTY as any other U macros only works in UObjects and only UObject classes getting reflected so it useless. But this also rises other issue, engine won’t see your UUserWidget* reference, which means whatever you create and refrence only here is a subject of garbage cleaning. You can prevent that you need to set flag to the UObject SetFlags(RF_MarkAsRootSet), remeber that this won’t be GC so everything oyu will refrence there never gonna be colelcted too so watch out what you doing there or lese oyu might get yourself to seriues memory leak issues, it is best to find some UObject in editor features to host it instead

For UObjects outside of UObject classes you should also use special pointer TWeakObjectPtr which save you form any invalid pointer issues

Also UMG and blueprint in general was not made to work in editor-time, UMG was made to work on gameplay viewport so there high chances that what you trying to do won’t work. TakeWidget() will get you widget data but scripting for them that sit in blueprints won’t work.

Fisrt of all thanks a lot for your time. Im kinda new to c++ so i dont understand much of your are saying :c I just need to make this work and im good. I forgot to mention that im trying to use the code of this plugin (UNREAL ENGINE 4 TOOL | UMG TO SLATE (4.15 UPDATE) - YouTube) I cant run it in 4.19 bcs it only works 4.15.

If i understand your solution, i need to change my createdWidget to TWeakObjectPtr createdWidget = nullptr; ?
and also add UObject SetFlags(RF_MarkAsRootSet)

I did try your solution. Im not getting the same error now instead im getting this:

Severity	Code	Description	Project	File	Line	Suppression State
Error	C2061	syntax error: identifier 'RF_MarkAsRootSet'	BuildingEscape	C:\repos\03_building_escape\BuildingEscape\Plugins\teste\Source\teste\Public\teste.h	30	
Error (active)	E0757	constant "RF_MarkAsRootSet" is not a type name	BuildingEscape	C:\repos\03_building_escape\BuildingEscape\Plugins\teste\Source\teste\Public\teste.h	30	

I added

#include "Runtime/CoreUObject/Public/UObject/ObjectMacros.h"

And also change the pointer lines to

UObject SetFlags(RF_MarkAsRootSet);
	TWeakObjectPtr<UUserWidget> createdWidget = nullptr;

I has the same problem and it’s solved here: c++ - I'm having problems with UUserWidgets on UE5 - Stack Overflow

3 Likes