虛幻引擎UE4如何鏈接第三方庫(lib和dll)
2020-11-9 21:46:41??????點擊:
在開發(fā)中經常遇到的問題就是加入某第三方庫的支持,這樣的第三方庫往往屬于無源碼,而且可能是靜態(tài)lib或者是動態(tài)dll甚至兩者皆有。UE4的編譯管理用的是自己的UBT(unreal binary tool)因此鏈接第三方庫的工作主要是編寫UBT腳本。
1.以插件方式集成.
基本上這個是優(yōu)先推薦的集成第三方庫的方式,因為能夠很好的隔離你的代碼和第三方代碼的影響,在UE4的源碼里也可以看到很多第三方庫都是這么集成的,比如paper2D,leapmotion等等。在UE4中新建插件的方式略去不表,當你新建完你的插件之后,你會在插件的代碼目錄下看到一個xxx.build.cs
基本上這個是優(yōu)先推薦的集成第三方庫的方式,因為能夠很好的隔離你的代碼和第三方代碼的影響,在UE4的源碼里也可以看到很多第三方庫都是這么集成的,比如paper2D,leapmotion等等。在UE4中新建插件的方式略去不表,當你新建完你的插件之后,你會在插件的代碼目錄下看到一個xxx.build.cs
接下來要做的就是修改這個腳本:
得到當前路徑
-
private string ModulePath
-
{
-
get { return ModuleDirectory; }
- }
關于第三方庫放的位置,一般是在plugin的源碼同級文件夾下建一個ThirdParty文件夾,里面放上include lib等等。
得到ThirdParty文件夾的路徑
-
private string ThirdPartyPath
-
{
-
get { return Path.GetFullPath(Path.Combine(ModulePath,"../../ThirdParty/")); }
- }
為工程添加include第三方庫的頭文件路徑
在??斓臉嬙旌瘮?shù)里加上:
在??斓臉嬙旌瘮?shù)里加上:
-
PublicIncludePaths.AddRange(
-
new string[] {
-
Path.Combine(ThirdPartyPath, "xxx", "Include"),
-
}
-
);
-
-
-
PrivateIncludePaths.AddRange(
-
new string[] {
-
Path.Combine(ThirdPartyPath, "Foxit", "Include"),
-
}
- );
鏈接第三方庫的Lib
接下來需要在編譯工程時加入第三方靜態(tài)庫的鏈接,靜態(tài)鏈接屬于工程在編譯期間做的事情,因此這塊需要通過cs腳本完成,而dll動態(tài)鏈接庫的加載是運行期的事,因此需要在cpp文件中執(zhí)行。
我們新建一個叫LoadxxxLib的函數(shù),并把它放在模塊的構造函數(shù)結尾執(zhí)行:
-
public bool LoadxxxLib(TargetInfo Target)
-
{
-
bool isLibararySupported = false;
-
if ((Target.Platform == UnrealTargetPlatform.Win64) || (Target.Platform == UnrealTargetPlatform.Win32))
-
{
-
isLibararySupported = true;
-
string PlatformString = (Target.Platform == UnrealTargetPlatform.Win64) ? "Win64" : "Win32";
-
PublicAdditionalLibraries.Add(Path.Combine(LibraryPath, PlatformString + ".lib"));
-
PublicDelayLoadDLLs.Add(PlatformString + ".dll");
-
RuntimeDependencies.Add(new RuntimeDependency(LibraryPath + PlatformString + ".dll"));
-
}
-
return isLibararySupported;
- }
這樣就可以保證在編譯期鏈接上我們的第三方lib。
鏈接動態(tài)DLL
這個工作需要在plugin的運行期完成,在插件的source文件下找到一個與插件名字同名的cpp文件打開。會看到一個StartupModule的函數(shù),我們需要在這里得到dll文件的handle。
在StartupModule中添加下面的代碼:
-
void FXXXModule::StartupModule()
-
{
-
#if PLATFORM_64BITS
-
FString platform = TEXT("win64.dll");
-
#else
-
FString platform = TEXT("win32.dll");
-
#endif
-
FString path = IPluginManager::Get().FindPlugin("XXX")->GetBaseDir();
-
FString dllpath = path + "/ThirdParty/XXX/Lib/" + platform;
-
PdfDllHandle = FPlatformProcess::GetDllHandle(*dllpath);
-
if (!PdfDllHandle)
-
{
-
UE_LOG(LogTemp, Warning, TEXT("Failed to load PDF library."));
-
}
- }
這里我們用的是PluginManager找到的插件所在的路徑,值得注意的是使用這個函數(shù)時需要在build.cs中加入
-
PrivateDependencyModuleNames.AddRange(
-
new string[]
-
{
-
...
-
"Projects",
-
}
- );
否則工程會鏈接出錯。
- 上一篇:通過UE4 的 INTEL REALSENSE 插件以新的方 2020/11/9
- 下一篇:HoloLens 2和一代HoloLens有什么差別 2020/11/3


