Using Boost in UE4 Project

You need to create Boost module (see Engine/Sources/ThirdParty for a plenty of examples) which will have a Boost.Build.cs file. Its contents would be something along these lines

using UnrealBuildTool;
using System;
using System.IO;

public class Boost : ModuleRules
{
		public Boost(ReadOnlyTargetRules Target) : base(Target)
		{
				Type = ModuleType.External;					
				PublicSystemIncludePaths.AddRange(
						new string[] {
								"/boost/include/headers"
								}
						);

				if (Target.Platform == UnrealTargetPlatform.Linux)
				{
					PublicAdditionalLibraries.Add("boost_system");
					PublicAdditionalLibraries.Add("boost_foo");
					PublicAdditionalLibraries.Add("boost_bar");
				}
		}
}

Note that you can include system headers/libs here, however, this is not recommended. In order for the build to be reproducible on other systems I recommend bundling the headers and libs. This also includes runtime libs which should be able to run on the oldest Linux distro you intend to target.

If you are not bundling them, it’s up to you to make sure that the users of your build have the binary compatible versions installed.

I am currently trying to migrate my C++ project to Unreal. I used several Boost libraries (filesystem, regex, a.s.o).
I added all relevant boost headers to my .h files in Unreal. Of course, I get an linker error:

undefined symbol: _ZN5boost6system16generic_categoryEv

In CMake I can simply add:

find_package(Boost COMPONENTS chrono system filesystem thread regex random REQUIRED)

target_link_libraries (myprog
 ...
  ${Boost_FILESYSTEM_LIBRARY}
  ${Boost_SYSTEM_LIBRARY}
  ${Boost_THREAD_LIBRARY}
  ${Boost_REGEX_LIBRARY}
  ${Boost_RANDOM_LIBRARY}
  ${Boost_GRAPH_LIBRARY}
  ${Boost_CHRONO_LIBRARY}
)

Is there any way to tell Unreal which Boost libs to link?

Both approaches sound great. I am still unsure if I understand what you mean by “create a Boost module”. Please correct me if I am wrong:

  1. Create a folder like ~/UnrealProjects/myproject/ThirdParty/BoostInternalLibrary
  2. Create a folder “includes” in this new folder.
  3. Copy all files from /usr/include/boost/ to includes
  4. edit /UnrealProjects/myproject/Source/myproject/myproject.Build.cs
    → Modify the file according to your subscription.

Question: It looks to me like the examples in the UnrealEngine have somehow precompiled .a / .so / .whatever files. Do I have to create these also for boost or can Unreal compile these?

Question2: Will I be able to use the boost lib in my Unreal c++ source with the same syntax then before or do I have to add some undocumented Unreal syntax to add each library function?

Ok, forget my previous questions. I just want to know if I have to create a module to compile a standalone boost lib for all modules in boost that I need? (which are many)

If so, please tell me.

(I am thankfull for your answer but I am not skilled enough to build boost as a standalone lib AND integrate it into the Unreal Build Process without any documentation. I tried to create a module with your example but I still get the error:

LogLinux:Warning: dlopen failed: /path/UnrealProjects/myproject/Binaries/Linux/libUE4Editor-myproject.so: undefined symbol: _ZN5boost6system16generic_categoryEv

I need to know if there is any documented way to include boost to my project. )

If you don’t intend to ship your binary, just add an empty module with the Boost.Build.cs as I described above. It will refer to the system libs and will work on your system.

In general, if you intend to ship your binary, you need to solve the problem of making sure that the user has compatible boost libs anyway, no matter if you are shipping with Unreal or without it. So you can apply the same solution that you would without Unreal, be it having boost installed as a dependency of your package or bundling the boost libs with your program.

I wish it would be as easy as you instructed. I created a “boost” folder in the Third Party Folder of my project.
I added the Boost.build.cs with the content you mentioned. I adjusted the “/boost/include/headers” to “/usr/include/boost”.

Sadly, the error remains exactly the same. I removed the /UnrealProjects/myproject/Binaries/Linux/libUE4Editor-myproject.so to force Unreal to rebuild the module. No change either.

I am stuck… :frowning:

Did you add library names? You need to find the names of the .so (or .a) files that you are linking against (i.e. variables like ${Boost_FILESYSTEM_LIBRARY} in your CMake script) and add them via PublicAdditionalLibraries.Add(); like in example above.

So for instance, if the ${Boost_SYSTEM_LIBRARY} refers to libboost_system.so, then add

PublicAdditionalLibraries.Add(“boost_system”);

Sorry for the delay. Busy world. Yes I added the required Library name. In this case, exactly the line you mention. Still, when starting the UE4 engine I get:

LogLinux:Warning: dlopen failed: UnrealProjects/myproject/Binaries/Linux/libUE4Editor-myproject.so: undefined symbol: _ZN5boost6system16generic_categoryEv

I use “nm” on the .so file to check for the missing library and it returns:
U _ZN5boost6system16generic_categoryEv
Normally, the first column should list the memory adress for the corresponding function. The line indicates that linking did not take place :frowning:

Do I have to tell myproject somehow to include this ThirdParty Module?
I added the Boost Module in myProject.Target.cs and myProjectEditor.target.cs in the line:
OutExtraModuleNames.AddRange( new string[] { “myProject”, “Boost” } );

Now, when compiling, make fails with the error:
ERROR: Couldn’t find module rules file for module ‘Boost’.
Why does it not find the Boost Module in the ThirdParty Folder?