• 您的位置:首頁(yè) > 新聞動態 > UE4

    虛幻UE4如何鏈接(jiē)第三方庫(lib和dll)

    2018/3/20      點擊:
    摘要:寫這個文章主要是被UE官方的wiki和answerhub誤導了很久,這本來是一個很常見和基(jī)本的問題,但是無論是官方的wiki或者是論壇上的提問都十分散亂並且充斥各種錯誤,因此記錄下這個(gè)在開發中時常遇到的問題。
    在(zài)開發中經(jīng)常遇到的問題就是加入某第三方庫的(de)支持(chí),這(zhè)樣的第三(sān)方庫往往屬於無源碼,而且可能是靜態lib或者是動態dll甚至兩者(zhě)皆有。UE4的編譯管理用的是自己的UBT(unreal binary tool)因此鏈接第三方庫的工作主要是編寫UBT腳本。
    1.以(yǐ)插件方式集成.
    基本上這個是*推薦的集成第三方庫的方式,因為能夠(gòu)很好的隔離(lí)你的代(dài)碼和第三(sān)方代碼的影響,在UE4的源碼裏也可以看到很多第三方庫都(dōu)是這麽集成的,比如paper2D,leapmotion等等。在UE4中新建插件的方式略去不表,當你新建(jiàn)完你的插件之後,你會(huì)在插件的代碼目錄下看到一個
    xxx.build.cs
    接下來要做的就是修改這個腳本:
    得到當前路徑
    1. private string ModulePath
    2. {
    3.    get { return ModuleDirectory; }
    4. }
    關於第三方(fāng)庫放的位置,一般是在plugin的源碼同級文件夾下建一個ThirdParty文件夾,裏麵放上include lib等等
    。得到ThirdParty文件夾的(de)路(lù)徑
    1. private string ThirdPartyPath
    2. {
    3.         get { return Path.GetFullPath(Path.Combine(ModulePath,"../../ThirdParty/")); }
    4. }
    為(wéi)工程添加include第三方庫的頭文件路徑(jìng)
    在模快的構造函數裏加上:
    1. PublicIncludePaths.AddRange(
    2.         new string[] { 
    3.              Path.Combine(ThirdPartyPath, "xxx", "Include"),
    4.         }
    5.         );
    6.             
    7.  
    8. PrivateIncludePaths.AddRange(
    9.         new string[] {
    10.             Path.Combine(ThirdPartyPath, "Foxit", "Include"),
    11.         }
    12.         );
    鏈接第三方庫的Lib
    接下來需要(yào)在編譯工程時加(jiā)入第三方靜態庫的鏈(liàn)接,靜態鏈接屬(shǔ)於工(gōng)程在編譯(yì)期間做的事情,因此這塊需要通過cs腳本完成(chéng),而dll動態鏈(liàn)接庫的加載是運行期的事,因此需要在cpp文件中執行。
    我們新建一個叫LoadxxxLib的函數,並(bìng)把它放(fàng)在模塊的構(gòu)造函數結尾執行:
    1. public bool LoadxxxLib(TargetInfo Target)
    2.     {
    3.         bool isLibararySupported = false;
    4.         if ((Target.Platform == UnrealTargetPlatform.Win64) || (Target.Platform == UnrealTargetPlatform.Win32))
    5.         {
    6.             isLibararySupported = true;
    7.             string PlatformString = (Target.Platform == UnrealTargetPlatform.Win64) ? "Win64" : "Win32";
    8.             PublicAdditionalLibraries.Add(Path.Combine(LibraryPath, PlatformString + ".lib"));
    9.             PublicDelayLoadDLLs.Add(PlatformString + ".dll");
    10.             RuntimeDependencies.Add(new RuntimeDependency(LibraryPath + PlatformString + ".dll"));
    11.         }
    12.         return isLibararySupported;
    13.     }

    這樣就可以保證在編譯(yì)期鏈接上我們(men)的第三方lib。


    鏈接動態(tài)DLL
    這個工作需要在plugin的運行期完成(chéng),在插件的source文件下找到一個與(yǔ)插件名字同名的cpp文件打開。會看到一(yī)個StartupModule的(de)函數,我們需要(yào)在這裏得到dll文件的handle。

    在StartupModule中添(tiān)加下麵的代碼:

    1. void FXXXModule::StartupModule()
    2. {
    3. #if PLATFORM_64BITS
    4.     FString platform = TEXT("win64.dll");
    5. #else
    6.     FString platform = TEXT("win32.dll");
    7. #endif
    8.     FString path = IPluginManager::Get().FindPlugin("XXX")->GetBaseDir(); 
    9.     FString dllpath = path + "/ThirdParty/XXX/Lib/" + platform;
    10.     PdfDllHandle = FPlatformProcess::GetDllHandle(*dllpath);
    11.     if (!PdfDllHandle)
    12.     {
    13.         UE_LOG(LogTemp, Warning, TEXT("Failed to load PDF library."));
    14.     }
    15. }
    這裏我們用的是PluginManager找到的插件所在的路徑,值得注意的是使用這個函數時需要在build.cs中加入
    1. PrivateDependencyModuleNames.AddRange(
    2.             new string[]
    3.             {
    4.                 ...
    5.                 "Projects",
    6.             }
    7.             );

    否則工程會鏈接出錯。


    自愉色色,亚洲色孩,jk无遮拦,日日夜夜7799天天综合,999夜夜,7799天天综合天天综合网精品视频,日日夜夜精品视频7799,天天综合7799精品影视