首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误LNK2001:未解析的外部符号虚幻引擎4使用插件(UE4)

错误LNK2001:未解析的外部符号虚幻引擎4使用插件(UE4)
EN

Stack Overflow用户
提问于 2022-01-14 00:28:37
回答 1查看 2.5K关注 0票数 1

我正在尝试为UE4导入一个第三方库,并在参与者中使用它。UE4文档建议创建一个插件来实现这一点,并提供了一个自动生成的模板,我目前正在尝试测试这个模板。

我创建了一个空白的C++ UE4项目,其中包含一个新的第三方自定义插件和一个可以在其中使用插件/库的参与者类。

这是我的主要项目的build.cs文件:

代码语言:javascript
复制
// Copyright Epic Games, Inc. All Rights Reserved.

using UnrealBuildTool;

public class TestGame : ModuleRules
{
    public TestGame(ReadOnlyTargetRules Target) : base(Target)
    {
        PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
    
        PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "CustomTestPlugin" });

        PrivateDependencyModuleNames.AddRange(new string[] { "CustomTestPlugin" });
        PublicIncludePaths.AddRange(new string[] { "../Plugins/CustomTestPlugin/Source/CustomTestPlugin/Public" });
        // Uncomment if you are using Slate UI
        // PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });

        // Uncomment if you are using online features
        // PrivateDependencyModuleNames.Add("OnlineSubsystem");

        // To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
    }
}

并将插件包含在参与者cpp文件中,并使用

代码语言:javascript
复制
#include "CustomTestPlugin.h"

以及在同一个文件中实例化它:

代码语言:javascript
复制
    FCustomTestPluginModule pluggin = FCustomTestPluginModule::FCustomTestPluginModule();

当我在没有上述条件的情况下编译时,它构建得很好,但是当我声明插入变量时,它会出现以下错误:

代码语言:javascript
复制
TestActor.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl FCustomTestPluginModule::StartupModule(void)" (?StartupModule@FCustomTestPluginModule@@UEAAXXZ)

H如下所示:

代码语言:javascript
复制
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "Modules/ModuleManager.h"

class FCustomTestPluginModule : public IModuleInterface
{
public:

    /** IModuleInterface implementation */
    //FCustomTestPluginModule();
    virtual void StartupModule() override;
    virtual void ShutdownModule() override;

private:
    /** Handle to the test dll we will load */
    void*   ExampleLibraryHandle;
};

cpp文件:

代码语言:javascript
复制
// Copyright Epic Games, Inc. All Rights Reserved.

#include "CustomTestPlugin.h"
#include "Core.h"
#include "Modules/ModuleManager.h"
#include "Interfaces/IPluginManager.h"
#include "CustomTestPluginLibrary/ExampleLibrary.h"

#define LOCTEXT_NAMESPACE "FCustomTestPluginModule"


//FCustomTestPluginModule::FCustomTestPluginModule() {
//  UE_LOG(LogTemp, Warning, TEXT("CustomPluConstructed"));
//}

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

    // Get the base directory of this plugin
    FString BaseDir = IPluginManager::Get().FindPlugin("CustomTestPlugin")->GetBaseDir();

    // Add on the relative location of the third party dll and load it
    FString LibraryPath;
#if PLATFORM_WINDOWS
    LibraryPath = FPaths::Combine(*BaseDir, TEXT("Binaries/ThirdParty/CustomTestPluginLibrary/Win64/ExampleLibrary.dll"));
#elif PLATFORM_MAC
    LibraryPath = FPaths::Combine(*BaseDir, TEXT("Source/ThirdParty/CustomTestPluginLibrary/Mac/Release/libExampleLibrary.dylib"));
#elif PLATFORM_LINUX
    LibraryPath = FPaths::Combine(*BaseDir, TEXT("Binaries/ThirdParty/CustomTestPluginLibrary/Linux/x86_64-unknown-linux-gnu/libExampleLibrary.so"));
#endif // PLATFORM_WINDOWS

    ExampleLibraryHandle = !LibraryPath.IsEmpty() ? FPlatformProcess::GetDllHandle(*LibraryPath) : nullptr;

    if (ExampleLibraryHandle)
    {
        // Call the test function in the third party library that opens a message box
        ExampleLibraryFunction();
    }
    else
    {
        FMessageDialog::Open(EAppMsgType::Ok, LOCTEXT("ThirdPartyLibraryError", "Failed to load example third party library"));
    }
}

void FCustomTestPluginModule::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.

    // Free the dll handle
    FPlatformProcess::FreeDllHandle(ExampleLibraryHandle);
    ExampleLibraryHandle = nullptr;
}

#undef LOCTEXT_NAMESPACE
    
IMPLEMENT_MODULE(FCustomTestPluginModule, CustomTestPlugin)

任何解决这一问题的帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2022-01-14 20:55:31

您不需要手动实例化模块。如果在项目中引用该模块,则该模块将在启动时由引擎加载和实例化。

如果由于某种原因需要获得对模块的单例的引用,可以使用

代码语言:javascript
复制
FModuleManager::LoadModuleChecked<FYourModuleType>("YourModule");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70704748

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档