pengquynh
pengquynh

Reputation: 23

How can I use the third party library, Eigen, in Unreal Engine?

I'm working on an UE4 plugin and want to use the Eigen library. It appears that UE4 has already integrated the library, which you can see in Engine>Source>ThirdParty>Eigen.

I looked at other plugins, such as AlembicImporter, for guidance. To use Eigen, I see that they add "Eigen" in the build.cs file and write #include <Eigen/...> in the source files that use Eigen, where ... could be either Dense, SVD, Sparse, etc.

I tried this, but when I build my project, I get the error

fatal error: 'Eigen/Dense' file not found

(Similarly, I get an error for any Eigen/... I try to use)

Upvotes: 2

Views: 2502

Answers (2)

Sengchor Taing
Sengchor Taing

Reputation: 1

I found the solution. We don't need to download the Eigen library for inclusion in the plugin; we can simply use Unreal Engine's built-in version for UE5.4. When you use the Eigen library from a download, it works in the editor, but after packaging the project and opening it, the project crashes when calling functions related to Eigen. I believe this is caused by Unreal Engine's memory allocation being different from that of the Eigen library. In YourPlugin.Build.cs:

// Use Unreal Engine built-in Eigen as a third-party dependency
AddEngineThirdPartyPrivateStaticDependencies(Target, "Eigen");

If you encounter an error during the project packaging related to unreachable code, you can suppress the error and continue compiling.

Also in YourPlugin.Build.cs:

// Suppress compiler warning C4702 for unreachable code
PublicDefinitions.Add("EIGEN_IGNORE_UNREACHABLE_CODE_WARNING=1");

To suppress the warning in your local code:

#pragma warning(push)
#pragma warning(disable : 4702) // Disable unreachable code warning

#include "Eigen/Dense"

#pragma warning(pop)

Upvotes: 0

Wacov
Wacov

Reputation: 422

Looks like prebuilt UE4 doesn't include the "compiled" Eigen headers ("Dense", "Sparse", etc.), though it does include the Eigen "src" folder.

If you compile your Engine from source, you should have a complete Eigen install in the ThirdParty folder. You can then use that just like various Engine plugins do. But compiling Unreal from source is a bit of a pain and takes a lot of hard drive space. This would also prevent you from distributing your plugin in source form, as normal users wouldn't have Eigen available.

It's probably easiest to include a local copy of Eigen with your plugin - it's just headers, so you can include it with just a private include path in the build.cs.

Upvotes: 4

Related Questions