虛幻UE4如何鏈接第三方庫(lib和dll)
2018/3/20 點擊:
摘要:寫這個文章(zhāng)主要是被UE官方的wiki和answerhub誤導了很久(jiǔ),這本來(lái)是一個很常見和基本(běn)的問題(tí),但是無論是官方的wiki或者是論壇上的提問都十分(fèn)散亂並且充斥各種(zhǒng)錯誤,因此記錄下這個在開發中時常遇到的問題。
在開發中經常遇到的問題就是加入某第三方庫(kù)的支持,這(zhè)樣的第三方庫往往屬於無源碼,而且可能是靜態lib或者(zhě)是動態dll甚至兩者皆有。UE4的編譯管理用的(de)是自己的UBT(unreal binary tool)因此鏈接第三方(fāng)庫的工作主要是(shì)編寫UBT腳本。
1.以插件方式集成.
基本上這個是*推薦的集成第三方庫的方式,因為能夠很好的隔離你的代碼和第三方代碼的影響,在UE4的源碼裏(lǐ)也(yě)可以看到很多第三方庫都是這麽集成的,比如paper2D,leapmotion等(děng)等。在UE4中新建插(chā)件的方式略去不表,當你新建完你的插件之後,你會在插件的代(dài)碼目錄下看到一個(gè)
xxx.build.cs
基本上這個是*推薦的集成第三方庫的方式,因為能夠很好的隔離你的代碼和第三方代碼的影響,在UE4的源碼裏(lǐ)也(yě)可以看到很多第三方庫都是這麽集成的,比如paper2D,leapmotion等(děng)等。在UE4中新建插(chā)件的方式略去不表,當你新建完你的插件之後,你會在插件的代(dài)碼目錄下看到一個(gè)
接下來要做(zuò)的就是修改這個腳本:
得到當前路徑
-
private string ModulePath
-
{
-
get { return ModuleDirectory; }
- }
關於(yú)第三方庫放的位置,一(yī)般是(shì)在plugin的源碼同級文件夾(jiá)下建一個(gè)ThirdParty文件夾,裏麵(miàn)放(fàng)上include lib等等
。得到ThirdParty文件夾的路徑
-
private string ThirdPartyPath
-
{
-
get { return Path.GetFullPath(Path.Combine(ModulePath,"../../ThirdParty/")); }
- }
為工程添加include第三方庫的(de)頭文件路徑
在模快的構造函數裏加上:
在模快的構造函數裏加上:
-
PublicIncludePaths.AddRange(
-
new string[] {
-
Path.Combine(ThirdPartyPath, "xxx", "Include"),
-
}
-
);
-
-
-
PrivateIncludePaths.AddRange(
-
new string[] {
-
Path.Combine(ThirdPartyPath, "Foxit", "Include"),
-
}
- );
鏈接第三方庫的Lib
接下來需要在編譯(yì)工程時加入第(dì)三方(fāng)靜態庫的鏈接,靜態鏈接屬於工程在編譯期間做的事情,因此這塊需要通過cs腳本完成,而dll動態鏈接庫的(de)加載是運行(háng)期的(de)事(shì),因此需要(yào)在cpp文件中執行。
我(wǒ)們新(xīn)建一個叫LoadxxxLib的函數,並把它放在模塊的構造函數結尾執行:
-
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;
- }
這樣就可以(yǐ)保證(zhèng)在編譯期鏈(liàn)接(jiē)上(shàng)我們的第三方lib。
鏈接動態DLL
這個(gè)工作需要在plugin的運行期完成(chéng),在插件(jiàn)的source文件下找到(dào)一個與插(chā)件名字同名的cpp文(wén)件打開。會看到一個StartupModule的函數,我們需要在這(zhè)裏得到dll文件的handle。
在StartupModule中(zhōng)添加下麵的代碼:
-
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找到的插件所在的路徑,值得注意的是使用這個函數時需要在build.cs中加入
-
PrivateDependencyModuleNames.AddRange(
-
new string[]
-
{
-
...
-
"Projects",
-
}
- );
否則(zé)工(gōng)程會(huì)鏈接出錯。
- 上一(yī)篇:UE4插件,展示如何使用第三方庫製作UE4插件 2018/3/20
- 下一篇:UE4調用VR外設(shè)WISEGLOVE數據手套的LIB和DLL 2018/3/20