[RESOLVED] Referencing UMG Widgets in Code - error LNK2019

Hello,

looking to create a button in a HUD, linked to a method of a class, I followed this tutorial: https://wiki.unrealengine.com/UMG,_R…dding_Modules:
but when I copy the code provided, and I compile / generate the code, here is the error that Visual Studio gives me:
ps: sorry for the lines in French, the lines that pose me problems are those speaking of “error LNK2019”.


  C:\Users\jreix\Documents\Unreal Projects\GenerationProcedurale\Tests\GenProc_Texture\Binaries\Win64\UE4Editor-GenProc_Texture-3541.dll : fatal error LNK1120: 4 externes non r?solus
  Manette.gen.cpp.obj : error LNK2019: symbole externe non r?solu "__declspec(dllimport) class UClass * __cdecl Z_Construct_UClass_UUserWidget_NoRegister(void)" (__imp_?Z_Construct_UClass_UUserWidget_NoRegister@@YAPEAVUClass@@XZ) r?f?renc? dans la fonction "void __cdecl `dynamic initializer for 'public: static struct UE4CodeGen_Private::FClassPropertyParams const Z_Construct_UClass_AManette_Statics::NewProp_Generer''(void)" (??__E?NewProp_Generer@Z_Construct_UClass_AManette_Statics@@2UFClassPropertyParams@UE4CodeGen_Private@@B@@YAXXZ)
  Manette.cpp.obj : error LNK2019: symbole externe non r?solu "__declspec(dllimport) public: static class UUserWidget * __cdecl UUserWidget::CreateWidgetInstance(class APlayerController &,class TSubclassOf<class UUserWidget>,class FName)" (__imp_?CreateWidgetInstance@UUserWidget@@SAPEAV1@AEAVAPlayerController@@V?$TSubclassOf@VUUserWidget@@@@VFName@@@Z) r?f?renc? dans la fonction "class UUserWidget * __cdecl CreateWidget<class UUserWidget,class AManette>(class AManette *,class TSubclassOf<class UUserWidget>,class FName)" (??$CreateWidget@VUUserWidget@@VAManette@@@@YAPEAVUUserWidget@@PEAVAManette@@V?$TSubclassOf@VUUserWidget@@@@VFName@@@Z)
  Manette.cpp.obj : error LNK2019: symbole externe non r?solu "__declspec(dllimport) public: void __cdecl UUserWidget::AddToViewport(int)" (__imp_?AddToViewport@UUserWidget@@QEAAXH@Z) r?f?renc? dans la fonction "public: virtual void __cdecl AManette::BeginPlay(void)" (?BeginPlay@AManette@@UEAAXXZ)
  Manette.cpp.obj : error LNK2019: symbole externe non r?solu "__declspec(dllimport) private: static class UClass * __cdecl UUserWidget::GetPrivateStaticClass(void)" (__imp_?GetPrivateStaticClass@UUserWidget@@CAPEAVUClass@@XZ) r?f?renc? dans la fonction "class UUserWidget * __cdecl CreateWidget<class UUserWidget,class AManette>(class AManette *,class TSubclassOf<class UUserWidget>,class FName)" (??$CreateWidget@VUUserWidget@@VAManette@@@@YAPEAVUUserWidget@@PEAVAManette@@V?$TSubclassOf@VUUserWidget@@@@VFName@@@Z)

here is my two files where the code of the tutorial is (when I comment them, all compile fine) :

HEADER :


// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "Manette.generated.h"


class UUserWidget;

/**
 *
 */
UCLASS()
class GENPROC_TEXTURE_API AManette : public APlayerController
{
    GENERATED_BODY()

    public:
        // Note: that I am using forward declaration Because I am not including the
        // widget in the header and to prevent circular dependency.
        // You don't need to do that if you include the Widget Class in the .h
        // Forward declaration is just putting "class" before the class name so the compiler know it's a
        // class but it's not included in the header and don't freak out. Ex. “class UUserWidget”

        // Reference UMG Asset in the Editor
        UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Tests")
        TSubclassOf<UUserWidget> Generer;

        // Variable to hold the widget After Creating it.
        UUserWidget* GenBouton;

        // Override BeginPlay()
        virtual void BeginPlay() override;
};

SOURCE FILE :


// Fill out your copyright notice in the Description page of Project Settings.


#include "Manette.h"

#include "Blueprint/UserWidget.h"

void AManette::BeginPlay()
{
    Super::BeginPlay();

    if (Generer) // Check if the Asset is assigned in the blueprint.
    {
        // Create the widget and store it.
        GenBouton = CreateWidget<UUserWidget>(this, Generer);

        // now you can use the widget directly since you have a referance for it.
        // Extra check to  make sure the pointer holds the widget.
        if (GenBouton)
        {
            //let add it to the view port
            GenBouton->AddToViewport();
        }

        //Show the Cursor.
        bShowMouseCursor = true;
    }

}

I am not a veteran programmer, nor an expert on Unreal engine with C ++.
can you help me find the error or provide me with a simpler solution?

Thank you in advance for your answers.

2 Likes

I’m sorry for posting… My mistake was that I Put the line


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

in the wrong “build.cs” file… in another project…

20 Likes

Don’t be sorry. I had the same problem and this helped me solve it.

1 Like

Yeah, thats helped me too. Thanks a lot.

1 Like

Thanks a lot. Thats helped me too

1 Like

I am glad you posted this. I was stuck on this issue for the past 1 hour. This fixed my issue

OMG, You just saved me. Thank you for posting.

Never would have thought that referencing UWidget in CPP would have required a dependency module…

Because I am a noob as well, and this is the second time I’ve run into a dependency problem that took me hours to sort through, I’ll try to add more info (as best I can with limited experience) so that the next person can slingshot past where I’m at :slight_smile:

My build errors were as follows:

  unresolved external symbol "__declspec(dllimport) class UClass * __cdecl Z_Construct_UClass_UWidget_NoRegister(void)" (__imp_?Z_Construct_UClass_UWidget_NoRegister@@YAPEAVUClass@@XZ) referenced in function "void __cdecl `dynamic initializer for 'public: static struct UECodeGen_Private::FObjectPropertyParams const Z_Construct_UFunction_APoopiePlayerController_TrackOpDetailView_Statics::NewProp_View''(void)" (??__E?NewProp_View@Z_Construct_UFunction_APoopiePlayerController_TrackOpDetailView_Statics@@2UFObjectPropertyParams@UECodeGen_Private@@B@@YAXXZ)

and

1 unresolved externals

The Construct_UClass_UWidget_NoRegister(void), dynamic initializer for 'public: static struct and unresolved externals (I think), are the bread crumbs that would key you in on UWidget as a missing Public Dependency module.

The build error began when I tried to add a UFUNCTION(Blueprintcallable) function that referenced a UWidget in cpp.

For whatever reason, if I removed the UFUNCTION(Blueprintcallable), it would build but I wouldn’t be able to call that function from a blueprint (which is what I needed).

As the OP said, the answer, was to add “UMG” to the list in build.cs file on the line that has the PublicDependencyModuleNames.AddRange. In my setup, it was mycoolproject.Build.cs

I hope this helps someone else.

3 Likes

Don’t be sorry, you actually helped a lot.
I have met the same problem and fixed with this post.

It helped me as well. When this kind of error happen, what is the best course of action to find the module to be included?

this posting finis my problem,love from china!

Thanks this helped me too. I had totally forgot to add to the ´build.cs´ file.