How to include any Third-Party library in Unreal Engine 4

Nov, 12 2020— #advanced

If you are wondering how to include any open source library into your game for any platform, this tutorial is for you.

Let's abstract and think about what options we have:

  1. Directly connect all third-party library files (declaration headers "*.h" and definition files "*.cpp").
  2. Compile third-party libraries separately for each platform (e.g. for win32 and win64, or armv7 and arm64).

Both of these options have their advantages and disadvantages. But when we try to directly include all open source library files, we may have lots of different problems, such as namespace conflicts and built-in method implementation problems unique to each platform. Today we will look at the second example because this is much easier to implement when we use a medium- or large-sized library for different platforms.

Important notes:

First, you need to understand what platforms the game will be used for. If the platform for which you are compiling the library is the same as the target platform (for example, if you are doing on Windows x64 only for Windows x64), then you just need to compile the library in Visual Studio and go to the including library step. If not, use the following instruction.

Instruction by steps:

  1. Build the required files for the project described in the library documentation (for example, using CMake).
  2. Compile all required files using the correct compiler and toolchain file as needed. For example, for the android platform, you need to use the MinGW compiler and the official toolchain file from the Android NDK. For Windows, it's enough to compile in Visual Studio separately for each architecture (x32 and x64)
  3. Create plugin and include each library using the instruction below.

Now, you will have a ready headers and static library, which will probably have one of the following extensions: *.lib or *.a.

The next thing you need to create an empty plugin. How to do this is explained in this article.

Finally, all you have to do is to properly include it. You should copy-and-paste all of the headers and static librarie(s) to your plugin folder and in your [PluginName].Build.cs write the following code:

PrivateIncludePaths.AddRange("PluginFolder/LibraryHeadersFolder"); //to include headers for your library (*.h)

if (UnrealTargetPlatform.Win32 == Target.Platform || UnrealTargetPlatform.Win64 == Target.Platform) //decide the platform to package the binaries
{
      PublicAdditionalLibraries.Add(Path.Combine(ModuleDirectory, "LibraryBinariesFolder", "Windows", "liblibrary.lib")); //the full path is "LibraryBinariesFolder/Windows/liblibrary.lib"
}
else if (UnrealTargetPlatform.Android == Target.Platform)
{
     PublicAdditionalLibraries.Add(Path.Combine(ModuleDirectory, "LibraryBinariesFolder", "Android", "armeabi-v7a", "liblibrary.a")); //the full path is "LibraryBinariesFolder/Android/armeabi-v7a/liblibrary.a"
     PublicAdditionalLibraries.Add(Path.Combine(ModuleDirectory, "LibraryBinariesFolder", "Android", "arm64-v8a", "liblibrary.a")); //the full path is "LibraryBinariesFolder/Android/arm64-v8a/liblibrary.a"
}

Afterwards, in your [ProjectName].Build.cs you need to include your plugin:

PrivateIncludePathModuleNames.AddRange(new string[] { "PluginName" });

Afterwards you can use all the necessary methods from the library.

By Georgy Treshchev

Unreal Engine Developer. Contact for any questions. Telegram chat, e-mail, Github.