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

    2018/3/20      點擊:
    摘要:寫這個文章主要(yào)是被UE官方的wiki和answerhub誤導了很久,這本來是一個很常見和基本的問題,但是無論是官方的(de)wiki或者是論壇上的提問都十(shí)分散亂並且充斥各種(zhǒng)錯誤,因此記錄下這(zhè)個在開發中時常遇到的問題。
    在開發中經常遇到的問題就是加入某第三方庫(kù)的支持,這樣的第三方庫往往(wǎng)屬於無(wú)源(yuán)碼,而且可能是靜態lib或者是動態dll甚至兩(liǎng)者(zhě)皆有。UE4的編譯管理用的是自己的UBT(unreal binary tool)因此鏈接第三方庫的工作主要是編寫UBT腳本。
    1.以插件方式集成.
    基本上這(zhè)個是*推薦的集成第三方庫的(de)方式,因為能夠很好的隔離你的代碼和第三方代碼的影響,在(zài)UE4的源碼裏也可以看到很多第三方庫都是這麽集成的,比如paper2D,leapmotion等等(děng)。在UE4中(zhōng)新建插件的方式略去不(bú)表(biǎo),當你新建完你的插件(jiàn)之後,你會在插件的代碼(mǎ)目錄下看到一個
    xxx.build.cs
    接下(xià)來要做的就是修改這個腳本:
    得到當前路徑
    1. private string ModulePath
    2. {
    3.    get { return ModuleDirectory; }
    4. }
    關於第三方庫放(fàng)的位置,一般是在plugin的源碼同級文件夾下建一個ThirdParty文件夾,裏麵放上include lib等等
    。得到ThirdParty文件夾的(de)路徑
    1. private string ThirdPartyPath
    2. {
    3.         get { return Path.GetFullPath(Path.Combine(ModulePath,"../../ThirdParty/")); }
    4. }
    為工程添加include第三方庫的頭文件路徑
    在模快的構造函數裏加上:
    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
    接下來需要在編(biān)譯工程時加入第三方靜態庫的鏈接,靜態鏈(liàn)接屬於工程在(zài)編譯期(qī)間做的事情,因此這(zhè)塊需要通過cs腳本完成,而dll動態鏈接庫的加載是運行期的事,因此需要在cpp文(wén)件中執行。
    我們新建(jiàn)一個叫(jiào)LoadxxxLib的函數,並(bìng)把它放在模塊的構造函數(shù)結尾(wěi)執(zhí)行:
    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.     }

    這樣就可以保證(zhèng)在(zài)編譯(yì)期鏈接上我們的(de)第三方lib。


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

    在StartupModule中添加下麵的代碼:

    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. }
    這裏我們(men)用的是PluginManager找到的插件所在的(de)路徑,值得注意(yì)的(de)是使用這個函數(shù)時需要在build.cs中加入
    1. PrivateDependencyModuleNames.AddRange(
    2.             new string[]
    3.             {
    4.                 ...
    5.                 "Projects",
    6.             }
    7.             );

    否則(zé)工程會鏈接出錯。


    91影视免费版下载-91麻豆国产福利精品-91麻豆精品一二三区在线-国产91系列视频在线观看