Complete Example of UELibrary setup + extending the API

Complete Example of building and extending UELibrary

I’ve spent a lot of time figuring our how to set up UELibrary for my custom project. The official documentation is helpful but unfortunately incomplete. I’ve compiled all of my findings in one post + added an example on how you can extend the base API.

You can find the complete example here. I will outline the main points in this post:

  1. Get the source code for Unreal Engine. Compile it in the Development Editor configuration.
  2. Create a new Blank C++ Project. Let’s call it UELibExtended.
  3. Modify the UELibExtended.target.cs file per official documentation. You must add UELibrary module as well.
  4. Compile UELibExtended in Development configuration. It will produce UELibExtended.lib and UELibExtended.dll.
  5. Create a new Win32 app and link it against UELibExtended.lib. You’ll need to reference the UnrealEngine\Engine\Source\Runtime\UELibrary\Public\UELibraryAPI.h header in your app.
  6. When your project compiles copy UELibExtended.dll to the output directory.

To extend the base API you will need to create a new .h and .cpp file. Expose your functions using the __declspec(dllexport) syntax.

// .h file
#pragma once

#ifdef UELIBEXTENDED_LIBRARY_DLL_EXPORT
#define UELIBEXTENDEDLIBRARYAPI __declspec(dllexport)
#else
#define UELIBEXTENDEDLIBRARYAPI __declspec(dllimport)
#endif

extern "C"
{
    UELIBEXTENDEDLIBRARYAPI int UELibExtended_CountActors();
}
// .cpp file
#define UELIBEXTENDED_LIBRARY_DLL_EXPORT
#include "UELibExtendedApi.h"
#undef UELIBEXTENDED_LIBRARY_DLL_EXPORT

#include "EngineMinimal.h"

UELIBEXTENDEDLIBRARYAPI int UELibExtended_CountActors()
{
    if (!GEngine) {
        return 0;
    }

    for (const FWorldContext& Context : GEngine->GetWorldContexts())
    {
        if (Context.WorldType == EWorldType::Game) {
            return Context.World()->GetActorCount();
        }
    }
    return 0;
}

Compile your game in Development configuration and copy your .h file to include folder of your app.

3 Likes

Hi, @PaliasotMta.
I read your topic carefully and it was really helpful.
I followed your guide, but when the Unreal source is deleted, my application that embeds Unreal Engine as a library crashes.
What is the problem? Which dll files are missing?
Is it not possible to use it without the source code? If so, why did Epic make this feature?