我正在尝试为UE4导入一个第三方库,并在参与者中使用它。UE4文档建议创建一个插件来实现这一点,并提供了一个自动生成的模板,我目前正在尝试测试这个模板。
我创建了一个空白的C++ UE4项目,其中包含一个新的第三方自定义插件和一个可以在其中使用插件/库的参与者类。
这是我的主要项目的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
}
}并将插件包含在参与者cpp文件中,并使用
#include "CustomTestPlugin.h"以及在同一个文件中实例化它:
FCustomTestPluginModule pluggin = FCustomTestPluginModule::FCustomTestPluginModule();当我在没有上述条件的情况下编译时,它构建得很好,但是当我声明插入变量时,它会出现以下错误:
TestActor.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl FCustomTestPluginModule::StartupModule(void)" (?StartupModule@FCustomTestPluginModule@@UEAAXXZ)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;
};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)任何解决这一问题的帮助都将不胜感激!
发布于 2022-01-14 20:55:31
您不需要手动实例化模块。如果在项目中引用该模块,则该模块将在启动时由引擎加载和实例化。
如果由于某种原因需要获得对模块的单例的引用,可以使用
FModuleManager::LoadModuleChecked<FYourModuleType>("YourModule");https://stackoverflow.com/questions/70704748
复制相似问题