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

    虛(xū)幻UE4如(rú)何鏈接第三方庫(lib和dll)

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


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

    在StartupModule中添加下麵的(de)代碼(mǎ):

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

    否則工程會鏈接出錯。


    91网站入口_91视频导航_91短视频在线_91视频在线免费观看