UE4链接第三方库(lib和dll)

摘要:
写这个文章主要是被UE官方的wiki和answerhub误导了很久,这本来是一个很常见和基本的问题,但是无论是官方的wiki或者是论坛上的提问都十分散乱并且充斥各种错误,因此记录下这个在开发中时常遇到的问题。

在开发中经常遇到的问题就是加入某第三方库的支持,这样的第三方库往往属于无源码,而且可能是静态lib或者是动态dll甚至两者皆有。UE4的编译管理用的是自己的UBT(unreal binary tool)因此链接第三方库的工作主要是编写UBT脚本。

1.以插件方式集成.
基本上这个是最推荐的集成第三方库的方式,因为能够很好的隔离你的代码和第三方代码的影响,在UE4的源码里也可以看到很多第三方库都是这么集成的,比如paper2D,leapmotion等等。在UE4中新建插件的方式略去不表,当你新建完你的插件之后,你会在插件的代码目录下看到一个

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", "Include"),
        }
        );

链接第三方库的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",
            }
            );
            

否则工程会链接出错。

猜你喜欢

转载自blog.csdn.net/luofeixiongsix/article/details/80744970