UE4链接第三方库(lib和dll)
摘要: 在开发中经常遇到的问题就是加入某第三方库的支持,这样的第三方库往往属于无源码,而且可能是静态lib或者是动态dll甚至两者皆有。UE4的编译管理用的是自己的UBT(unreal binary tool)因此链接第三方库的工作主要是编写UBT脚本。 1.以插件方式集成. 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第三方库的头文件路径 PublicIncludePaths.AddRange( new string[] { Path.Combine(ThirdPartyPath,"xxx","Include"),} ); PrivateIncludePaths.AddRange( new string[] { Path.Combine(ThirdPartyPath,"Foxit",} ); 链接第三方库的Lib 接下来需要在编译工程时加入第三方静态库的链接,静态链接属于工程在编译期间做的事情,因此这块需要通过cs脚本完成,而dll动态链接库的加载是运行期的事,因此需要在cpp文件中执行。 我们新建一个叫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; } 这样就可以保证在编译期链接上我们的第三方lib。 链接动态DLL 这个工作需要在plugin的运行期完成,在插件的source文件下找到一个与插件名字同名的cpp文件打开。会看到一个StartupModule的函数,我们需要在这里得到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找到的插件所在的路径,值得注意的是使用这个函数时需要在build.cs PrivateDependencyModuleNames.AddRange( new string[] { ... "Projects",} ); 否则工程会链接出错。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- windows-server-2008-r2 – 组策略安装失败错误%?74
- Windows中的TortoiseSVN结帐问题
- iis-7 – 如何在Windows 2003 / IIS6上设置WebDeploy?
- 如何在Windows下暂停/恢复任何外部进程?
- 可以在Windows7x64(MSVC)和Linux64(GCC4.8.2)中的.cu文件(C
- windows-update – 如何防止wuauserv(Windows Update Servi
- 为什么Windows上的ActivePerl程序停止接受套接字连接?
- 【SoloPi】SoloPi使用1-初次使用,环境准备(Windows版)
- Windows通过组策略设置开机自启动脚本
- Windows – 如何编程检查“密码必须满足复杂性要求”组策略