Unreal Engine: Including a Third-Party Library (on the example of the Point Cloud Library and Boost) [Tutorial] [Download]

Unreal Engine: Including a Third-Party Library (on the example of the Point Cloud Library and Boost) [Tutorial] [Download]

Posted by admin | 27. Juli 2017 | Blog, Downloads, Tutorial

I recently tried to include the Point Cloud Library into Unreal. I ran into so many problems and had such a hard time finding proper answers/solutions (since Unreal’s documentation is kinda crappy regarding third party plugin creation) that I was close to burn Epic’s headquarters and once I finally figured things out, decided to make a small tutorial about including an external C++ library (on the example of the Point Cloud Library (PCL) and boost) into the Unreal Engine. I hope this’ll help some folk and will help prevent Epic’s headquarters to be burned in the future. 😀

General information:

I used the Unreal Engine 4.16, Visual Studio 2017, PCL 1.8.1 and boost 1.64; all 64-bit.

Download:

If you’re too lazy to read and just want a proper starting point for your third party plugin or just want to use boost/PCL functions in your project, you can download the template here:
https://github.com/ValentinKraft/Boost_PCL_UnrealThirdPartyPlugin


Including PCL & Boost into Unreal:

Before including PCL into Unreal, let’s start with doing the basic infrastructure, by creating an Unreal plugin. This plugin will consist out of two modules: The „PointCloudRenderer“ and the „PCL“ module. The external „PCL“ module will handle the import of the Third-Party libraries (= boost/PCL headers and libraries), while the „PointCloudRenderer“ will contain our core implementation. (Later, we will also create an interface module to call functions from the editor, see „Create a Connection to your Plugin in the Editor“). For making a plugin, I’d recommend you to use the built-in wizard (under Plugins –> New). Additionally, the documentation might help you with this (I decided for a „Non-Game Module“):
https://wiki.unrealengine.com/An_Introduction_to_UE4_Plugins
and/or here:
https://puppet-master.net/tutorials/unreal-engine-4/create-an-engine-plugin-using-blankplugin/

Inside of your just created Plugin’s Source folder you now have to create a „ThirdParty“ folder. Here you have to insert all the headers and libraries you want to include. For a minimal PCL project, you probably need the „Boost“ library and headers, the „Eigen“ library and the PCL headers and following libraries (if you do not know how to create the header/lib-files, have a look at the „Building PCL / Where to get the right .lib/.dll files from?“ section):

  • pcl_common_release.lib
  • pcl_io_release.lib
  • pcl_io_ply_release.lib
  • pcl_octree_release.lib
  • pcl_ml_release.lib

I chose the following folder structure for the third party assets:

  • [PluginDir]/Source/ThirdParty/[ThirdPartyLibName]/lib for the libraries and
  • [PluginDir]/Source/ThirdParty/[ThirdPartyLibName]/include for the headers.

Now you have to include the libraries into the Unreal/Visual Studio project. In Unreal, the UnrealBuildTool handles the import of all dependencies, so we have to tell the UnrealBuildTool to include the new libraries and headers via the corresponding .Build.cs-file. In your project, there should be already a „[PluginName].Build.cs“ (in my case, a PointCloudRenderer.Build.cs). What’s missing is the .Build.cs-file for our PCL module (we could also use the PointCloudRenderer.Build.cs, but its cleaner to do it in two separate Build-files), so you have to create a PCL.Build.cs with the following content:

And in the Plugin’s Build.cs file you just have to add your newly created „PCL“ module as a „PublicDependencyModule“:

And don’t forget to update the module section in your .uplugin-File:

Now you need to rebuild the Visual Studio project files using the Unreal build tool (for that, right-click on the uproject-File and choose „Generate Visual Studio project files“ or, when using the Unreal source version from Github, using the GenerateProjectFiles.bat). The Unreal build tool will include your PCL dependencies now into your plugin. And there you go! PCL is included in your plugin. Now we can include PCL/boost headers and call their functions from within Unreal (see line 46):

To now call our plugin’s functions from another script/class, we have to add the plugin’s functions to the plugin’s public IModuleInterface (for details see the full repo at Github). After including the plugin’s interface header file (in my case „IPointCloudRenderer.h“) in your script/class, we can now access our plugin’s functions simply via:

(Further information on linking libraries in Unreal can be found here):
https://wiki.unrealengine.com/Linking_Static_Libraries_Using_The_Build_System
and here:
https://wiki.unrealengine.com/How_to_Link_External_C_Libraries_.dll_.lib_With_Your_Project_%26_Package_With_Game,_Fast_And_Easy


Create a Connection/Component to your Plugin in the Editor:

We can now access our plugin from other scripts, hurray! But how do we include our new functionality into the Unreal editor? For example, it would be nice to have the plugin as a component with some public values we can change from the Editor just like this:

For this, we have to create a custom component. I’d recommend to, again, make a separate module that handles the communication between the Unreal Editor and our (already created) Plugin. For this, follow the instructions above (or, again, use the built-in wizard for Plugins under Plugins –> New). This module we will name „PointCloudRendererEditor“. Your file structure should now look like this:

First, update the .uplugin file with your new module:

The PointCloudRendererEditor’s Build.cs should now include your PointCloudRenderer module (line 23):

You can now create a new class (I’d recommend to do it from within Unreal) in your PointCloudRendererEditor module that will contain the actual custom component implementation (I named it „PointCloudRendererComponent“). As stated above we can now call our plugin’s functions like this:

Important is now the corresponding header file – in order to show up in the Editor, the class has to inherit from UActorComponent, has to be a UCLASS and the variables have to have the UPROPERTY flag/macro (in our case: filePath):

This, finally, brings the functionality now to the Editor:


Building PCL / Where to get the right .lib/.dll files from?:

To say this right from the start: Finding and including the right PCL .dll/.lib-files was by far the hardest part of the whole plugin creation. The documentation about this is really bad and I needed weeks to get it working properly. Since I finally included PCL succesfully, I’ll now let you know what I’ve learned.

The most important thing first: I wasn’t able to include PCL as a Dynamic Library (.dll) into Unreal. Maybe there was something wrong with the DLL, but it gave me access violation errors (see Trouble Shooting section). In the end, I included PCL as static libraries (.lib) and didn’t use DLLs at all.

So, in order for you to include your libraries into Unreal, you either find pre-built libraries in the internet (they have to be built in Release mode and with the right Visual Studio compiler version you use) or you build them yourself. I recommend you to build them yourself. I used CMAKE for that. As stated above, I only built static libraries (no DLLs).

For building the libraries, see the official instructions/tutorials on the corresponding official homepages. However, this page might be helpful as well:
http://vadim.macagon.com/blog/2014/10/29/build-a-ue4-compatible-static-lib-in-vs2013/
If you still want to try your luck including PCL DLLs, you can find the newest PCL all-in-one Installers here:
https://github.com/PointCloudLibrary/pcl/releases
For including DLLs however, I’d rather recommend to use Unreal’s Built-in wizard (Plugins –> New Plugin –> Third Party Plugin) since this involves a lot of special code for the DLL handling. Furthermore this might be helpful as well:
https://wiki.unrealengine.com/Linking_Dlls

Trouble shooting:

When you run into problems (which I did a lot), these information might help you:

  • Errors including „#pragma pack(pop)“ / C4103 error: make sure that you do the following in your Build.cs:
    • Enable RTTI with bUseRTTI = true (mandatory for the boost libraries)
    • Enable exception handling with bEnableExceptions = true
    • Add definitions for disabling the ABI headers: Definitions.Add(„BOOST_DISABLE_ABI_HEADERS=1“)
  • Errors including Access Violation, such as:

 

  • This is either a sign that you’re using a Debug version of your DLLs (especially with the _ITERATOR_DEBUG_LEVEL errors) (Unreal uses Release Runtimes only), so you have to build/install them again in Release mode (or maybe try the Github version of Unreal). As stated above, I don’t recommend to use DLLs with Unreal but rather static LIBs.
  • Or that there is a memory/heap corruption between the external library and Unreal or that certain objects are getting garbage collected by Unreal which then probably produce/delete nullpointers. I don’t know how to solve this kind of issues and I don’t recommend to use DLLs with Unreal but rather static LIBs, as stated above.
Blog Comments

Hi Ive been trying to get PCL working with UE4 as well. I got everything going in my plugin and Im at the point of trying to run the sample code from here http://pointclouds.org/documentation/tutorials/greedy_projection.php as soon as I declare the pcl::search::KdTree tree variable it crashes with an access violation. I dont even get a chance to use it, just declare the new tree and it crashes. Ive probably seen all your unreal forum posts when you were trying to get this going and I recall you saying everything was working except for some filter. So I was wondering if you could confirm for me that you can run this example code in unreal, specifically declaring and using a pcl::search::KDTree::Ptr variable. I did try to run the example code outside of unreal and it crashed there to at the same line. Im using PCL 1.8.1 and the latest of the 3rd party programs. Hoping you can help me out as PCL mailing list sucks really bad with pretty much ZERO help. Im not sure if its a PCL code issue or if its my enviroment setup. Thank you for your time and your tutorial in getting me this far.

Hey Chris!
Sorry, I am quite busy, so I didn’t test the whole greedy projection code you mentioned, but I tested the pcl::search::KDTree::Ptr variable successfully. For me, the declaration works and also a quick Nearest Neighbour Search worked as aspected. How are you including the PCL libraries? For me, dynamic libraries (DLLs) didn’t work, which is why I ended up using static libraries (.lib) which I included with e.g. PublicAdditionalLibraries.Add(Path.Combine(ModulePath, „PCL_181/lib/pcl_search_release.lib“));
I would recommend to do the same. If you still have problems, please just write me an email.
Cheers, Valle

[…] A tutorial […]

Add a comment

*Please complete all fields correctly

Related Blogs

Posted by admin | 16 Juli 2018
Download the Plugin for the Unreal Engine here: https://github.com/ValentinKraft/UE4_SortingComputeShader The compute shader that handles the sorting: [crayon-5c9a550a07ce2761657578/]  
Posted by admin | 27 Januar 2017
UnityViveAPI Single class that tries to implement an easy access to all HTC Vive sensor data/controls in Unity3D. To access the Vive data just add the Vive script to your…
Posted by admin | 20 November 2016
This little program/script allows you to generate a 3D Mesh out of a simple 2D Texture. Precisely, the 2D Texture will be upscaled to abitrary size using the "ImageQuilting" algorithm…