简体   繁体   English

错误 LNK2001:未解析的外部符号 Unreal Engine 4 Using Plugins (UE4)

[英]error LNK2001: unresolved external symbol Unreal Engine 4 Using Plugins (UE4)

I am trying to import a third party library for UE4 and use it in an actor.我正在尝试为 UE4 导入第三方库并在演员中使用它。 The UE4 documentation recommends creating a plugin to accomplish this and provides an auto generated template which i am trying to test currently. UE4 文档建议创建一个插件来完成此操作并提供一个自动生成的模板,我目前正在尝试对其进行测试。

I have created a blank C++ UE4 Project with a new third party custom plugin and an actor class in which to use the plugin/library.我创建了一个空白的 C++ UE4 项目,其中包含一个新的第三方自定义插件和一个演员 class,在其中使用插件/库。

This is my build.cs file for the main project:这是我的主项目的 build.cs 文件:


// 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
    }
}

and have included the plugin in the actor cpp file using并已将插件包含在演员 cpp 文件中,使用

#include "CustomTestPlugin.h"

as well as instantiating it in the same file:以及在同一个文件中实例化它:

    FCustomTestPluginModule pluggin = FCustomTestPluginModule::FCustomTestPluginModule();

when I compile without the above it builds fine but when i declare the pluggin variable it gives the following error:当我在没有上述内容的情况下编译时,它构建得很好,但是当我声明插件变量时,它给出了以下错误:

TestActor.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl FCustomTestPluginModule::StartupModule(void)" (?StartupModule@FCustomTestPluginModule@@UEAAXXZ)

CustomTestPlugin.h is as follows: CustomTestPlugin.h 如下:

// 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;
};

and the cpp file:和 cpp 文件:

// 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)


any help in resolving this would be greatly appreciated thanks!任何解决此问题的帮助将不胜感激,谢谢!

You should not need to instantiate your module manually.您不需要手动实例化您的模块。 The module is loaded and instantiated by the engine at startup if it is referenced in your project.如果在您的项目中引用了该模块,则引擎会在启动时加载并实例化该模块。

If for some reason you need to get a reference to your module's singleton, you can use如果由于某种原因您需要参考您的模块的 singleton,您可以使用

FModuleManager::LoadModuleChecked<FYourModuleType>("YourModule");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM