How to include sources without PCH include

All source files in module “XXX” must include the same precompiled header first.

What is the best way to include the sources of an external library without going through all files and add the PCH include? I don’t want to modify my libs just for the Unreal build system.

1 Like

If you want to include an external library without building it with the UE build system, put it in YourProject/ThirdParty/YourLibrary and tell the build system to use it inside YourProject.build.cs:

var third_party_path = Path.GetFullPath(Path.Combine(ModuleDirectory, "../../ThirdParty/"));
var your_lib_path = Path.Combine(third_party_path, "YourLibrary");

PublicIncludePaths.Add(Path.Combine(your_lib_path, "include"));
PublicAdditionalLibraries.Add(Path.Combine(your_lib_path, "lib/YourLibrary.lib"));
PublicDelayLoadDLLs.Add(Path.Combine(your_lib_path, "bin/YourLibrary.dll"));

For this example, we assume the headers are in YourLibrary/include.
On Windows, the .dll needs to be also present in YourProjects/Binaries/Win64.

You can see an example of including the OpenCV library in a plugin I am developing.

Thank you,

so the trick is to move it outside the Source folder, for example we could put ThirdParty on the same level as Source:

    PrivateIncludePaths.AddRange(
    			new string[] {
    				"MyPlugin/Private",
                    "../../MyPlugin/ThirdParty/glm"
    			}
    			);

Unfortunatly, this only works when including headers. You can’t add source files to compile this way, because the build system will remove the folder from the project as soon as you call “Generate XCode project”.

The XCode project is only there to power XCode as far as I know. The Unreal Build Tool doesn’t use it for compilation. It instead only uses the directory structure and the Build.cs files.

Path.Combine(ModuleDirectory, "../../ThirdParty/glm")

Yes, Unreal build system does not build the ThirdParty libraries, as it is usually intended to build them outside, or download a built package, and include the shared libraries.

Alternatively, you could do something like that:
MyLibrary.cpp

#include "MyLibraryHeader.h"
#include "MyProject.h" // the required precompiled header

#include "MyLibraryCode_1.cpp"
#include "MyLibraryCode_2.cpp"
...

Yes, I see,
I’m building my static library outside, that’s fine.
But I can’t find a way to tell the Unreal Build System (UBS) to link it.

If I put the path into PrivateDependencyModuleNames.AddRange, UBS is eager to build it and asks me for a module files. :wink:
ERROR: Couldn’t find module rules file for module ‘…/ThirdParty/HelloWorld/lib/libhelloworld’

So I looked again at the example gave. (thanks)
And it turned out, the best way is to link a static library to my plugin and build the library outside of the UBS.
For reference:
Put the following block into your public MyPlugin(TargetInfo Target) section.

PublicAdditionalLibraries.AddRange(
       new string[]
       {
             "/path_to_your_library/libHelloWorld.a"
       }
       );

The PublicAdditionalLibraries is not included in the plugin templates, I recommend to add it - Unreal can you hear me?
Thanks everybody for helping me.

Use PublicAdditionalLibraries for static libraries ( .lib , .a) and PublicDelayLoadDLLs for dynamic libraries ( .dll , .so).
PrivateDependencyModuleNames is for Unreal modules.