How to Link External C Libraries .dll .lib With Your Project & Package With Game, Fast And Easy

From Epic Wiki
Jump to: navigation, search

Overview

Author: Rama (talk)

Dear Community,

In this wiki I show you an utterly simple and fast way to get your external .lib working in UE4! My method has been tested and satisfies the following three requirements:

a. The external lib functions can be called in a WITH_EDITOR build, when run from commandline (UE4 Editor is closed).

b. The lib functions can be called from within the Editor in PIE, and the editor will actually load properly (Yay!).

c. The external lib functions will work in a packaged game!!!

Special Thanks

Special thanks go to my friend Greg who shared his research as well as the build.cs portion of this wiki!

Step 1: Put in YourGame/Binaries/Win64

Step 1 is to place your .lib and .dll files in

 YourProject/Binaries/Win64

next to your project's binaries.

Please note if you are packaging for win32 your external lib and dll files will need to go in your packaged Binaries/Win32 directory (see packaging step).

Step 2: Build cs

In your build cs you must tell UE4 where your .lib file is:

//In your build.cs
PublicAdditionalLibraries.Add(@"c:/YourProject/Binaries/Win64/YourLib.lib");

Step 3: Extern C

You must include the extern C block for your external .lib function definitions if they are in regular C, to prevent name mangling.

If you are trying to call C++ functions you do not need to do this :)

Please note this should usually be put inside of a #prama once .h file and #include in your relevant .cpp file

During testing you could place this at the top of the relevant .cpp file, but I would only recommend that for simple testing.

//Binaries/Win64/addtwo.lib
extern "C"
{
 __declspec(dllexport) int add(int x, int y);
}

Step 4: Calling your extern c functions

Now you simply call the function without any namespace:

	
UE_LOG(LogTemp,Warning,TEXT("static lib add %d"), add(5,5));

using namespace std;

If you are getting weird compile errors like fundamental c/c++ types missing, make sure to include this above your extern c block:

using namespace std;

//Binaries/Win64/addtwo.lib
extern "C"
{
 __declspec(dllexport) int add(int x, int y);
}

Step 5: Packaging With Your Game

When you package the game make sure to add the .dll and .lib files to:

 PackagedGameDir/YourGame/Binaries/Win64 or Win32

next to your game's packaged binary!

You pick Win64 or Win32 based on whichever one has your game's files in them, so this means you must have a version of your .lib that is appropriate for your build type. (ie, you need Win32 version of your external .lib if packaging a Win32 game).

Please do not confuse your project's actual .exe with the .exe that is placed in YourGame dir, your static libs must be placed inside of the Binaries folder structure.

Each time you package your game, if overwriting an existing packaged game, make sure your .dll and .lib files are still there!

Conclusion

This is the utterly simple way to get up and running with external libraries that will allow the editor to load, for you to test during development from commandline, and also works in packaged games!

My whole method revolves around learning that UE4 knows how to do all the linking properly when the .dll and .lib files are placed in the binaries folder along with your game's .exe / editor binaries.

Enjoy!

Rama (talk)