希望这篇文章能讲清一些问题,帮你找到答案!
目录
1.前提(简单的相关说明)
① ModuleDirectory :当前模块所在的目录
(D:\YourProject\Plugins\YourPlugin\Source\YourMoudle)
ModuleDirectory
② Path.Combine:路径拼接
(Path.Combine(”c“,"project") 返回 c/project)
Path.Combine(ModuleDirectory, "../ThirdParty/include")
③ 三方库文件类型,方便跨平台时统一标记
string libPrefix;
string libPostfix;
string DLLPostfix;
string platform;
if (Target.Platform == UnrealTargetPlatform.Win64)
{
platform = "Windows-x64";
libPostfix = ".lib";
libPrefix = "";
DLLPostfix = ".dll";
}
else if (Target.Platform == UnrealTargetPlatform.Mac)
{
platform = "Darwin-x64";
libPostfix = ".a";
libPrefix = "lib";
//DLLPostfix = ".dll"; 这个我不知道,目前只做过windows
}
else if (Target.Platform == UnrealTargetPlatform.Android)
{
platform = "Android-xaarch64";
libPostfix = ".a";
libPrefix = "lib";
//DLLPostfix = ".dll"; 这个我不知道,目前只做过windows
}
else if (Target.Platform == UnrealTargetPlatform.Linux)
{
platform = "Linux-x64";
libPostfix = ".a";
libPrefix = "lib";
//DLLPostfix = ".dll"; 这个我不知道,目前只做过windows
}
else if(Target.Platform == UnrealTargetPlatform.IOS)
{
platform = "iOS-xarm64";
libPostfix = ".a";
libPrefix = "lib";
//DLLPostfix = ".dll"; 这个我不知道,目前只做过windows
}
else {
platform = "Unknown";
libPostfix = ".Unknown";
libPrefix = "Unknown";
DLLPostfix = ".Unknown";
}
④ PublicDefinitions.Add("A=B") : 定义一个属于自己的宏,类似于引擎的WITH_EDITOR
PublicDefinitions.Add("YOUR_DLL_NAME=yourDLLName.dll");
2.引入静态库 .lib
引入Lib和DLL应该合并起来,此处为了表述清楚故而分开!!!
string LibPath = Path.Combine(ModuleDirectory, "../ThirdParty/lib/" + platform);
if (Target.Platform == UnrealTargetPlatform.Win64)
{
string LibName = "YourLib";
PublicAdditionalLibraries.Add(Path.Combine(LibPath, LibName + libPostfix));
}
else//other platform
{
}
3.引入动态库 .dll
引入Lib和DLL应该合并起来,此处为了表述清楚故而分开!!!
此处还自定义了三个相关的宏,用于Moudle链接DLL的统一方式
string DLLPath = Path.Combine(ModuleDirectory, "../ThirdParty/dll/" + platform);
if (Target.Platform == UnrealTargetPlatform.Win64)
{
string DLL1Name = "yourDLL1" + DLLPostfix;
string DLL2Name = "yourDLL2" + DLLPostfix;
PublicDelayLoadDLLs.Add(DLL1Name );
PublicDelayLoadDLLs.Add(DLL2Name );
RuntimeDependencies.Add(Path.Combine("$(TargetOutputDir)", DLL1Name ), Path.Combine(DLLPath, DLL1Name ));
RuntimeDependencies.Add(Path.Combine("$(TargetOutputDir)", DLL2Name ), Path.Combine(DLLPath, DLL2Name ));
//自定义宏
//我的DLL放在了ThirdParty/dll文件夹下
PublicDefinitions.Add("YOUR_DLL_PATH=Source/ThirdParty/dll/" + platform);
PublicDefinitions.Add("YOUR_DLL1_NAME=" + DLL1Name);
PublicDefinitions.Add("YOUR_DLL2_NAME=" + DLL2Name);
}
else//other platform
{
}
4.引入头文件 .h
一般会把引入的头文件放在模块同级目录的【ThirdParty/include】文件夹中,所以可以这么写,当然build.cs会自动生成一些文件,对比一下,手动添加即可;
PublicIncludePaths.AddRange(
new string[] {
// ... add public include paths required here ...
Path.Combine(ModuleDirectory, "../ThirdParty/include")
}
);
PrivateIncludePaths.AddRange(
new string[] {
// ... add other private include paths required here ...
}
);
5.Module链接动态库
Module文件中链接DLL
void FInVideoModule::StartupModule()
{
//#include "Interfaces/IPluginManager.h"
const FString PluginDir = IPluginManager::Get().FindPlugin(TEXT("YourPluginName"))->GetBaseDir();
const FString DLLPath = PluginDir / TEXT(PREPROCESSOR_TO_STRING(YOUR_DLL_PATH));
const FString DLL1Path = DLLPath / TEXT(PREPROCESSOR_TO_STRING(YOUR_DLL1_NAME));
const FString DLL2Path = DLLPath / TEXT(PREPROCESSOR_TO_STRING(YOUR_DLL2_NAME));
//定义在moudle.h中的变量
// void* Dll1Handle = nullptr;
// void* Dll2Handle = nullptr;
Dll1Handle = FPlatformProcess::GetDllHandle(*DLLPath);
Dll2Handle = FPlatformProcess::GetDllHandle(*DLLFFMPEGPath);
}