How to link *.lib and *.dll

Hello. I have a *.lib, *.h and *.dll files from third party library. I put it all to

Source/ThirdParty/MyLib

folder. In ModuleRules for *.h I specified relative path in PublicIncludePaths. This path looks like this:

PublicIncludePaths.AddRange(
new string {
“ThirdParty/MyLib/inc”
}
);

It works. Next I tried to specify *.lib relative path like this:

PublicAdditionalLibraries.Add(“ThirdParty/MyLib/lib/MyLib.lib”);

This not works. I specify for testing full path and it works. Obviously I can’t use full path for production, so what should I use?
And also I have a question about *.dll. I found some posts that says that currently I have to polute Binaries folder with *.dlls. Is it still true and there’s no way to specify *.dll path in build rules?

Hi,

did you see these ?
Static Libraries and Dynamic Libraries

Thank you. That helped partially.

For *.lib linking I’m using Path class in Build.cs. Don’t forget to include using System.IO;.

With *.dll all complicated. IN the example author uses single function and it works. Since I’m using 3d party library, it contains ton’s of classes and functions. I can’t declare every function as in this example. So for now I’m just place it in Binaries.

Obviously I can’t use full path for production, so what should I use?

Build an absolute path using the location of the Build.cs file, for example:

// path to directory containing this Build.cs file
var basePath = Path.GetDirectoryName(RulesCompiler.GetModuleFilename(this.GetType().Name));

PublicIncludePaths.Add(Path.Combine(basePath, "Public"));
PublicLibraryPaths.Add(Path.Combine(basePath, "..", "Build"));
PublicAdditionalLibraries.Add("MyLib.lib");

For a more complete example of wrapping a third-party static lib in a UBT module take a look here.

And also I have a question about .dll. I found some posts that says that currently I have to polute Binaries
folder with .dlls. Is it still true and there’s no way to specify *.dll path in build rules?

You may be able to get around this using FPlatformProcess::PushDllDirectory(), FPlatformProcess::GetDllHandle() and setting up your DLLs for delay loading in PublicDelayLoadDLLs (Build.cs), but I haven’t tried it, and it seems easier to just copy the DLLs to the binaries folder (using System.IO.File.Copy() in the Build.cs).

1 Like

Build an absolute path using the
location of the Build.cs file

That’s exactly how I did already.

Thank you for hints about dll, will try it.

I found this on Stack Overflow.
Very important.

"The .lib contains stubs for the functions etc. that are exported by the DLL. You link the .lib into your EXE and now your EXE knows how to call the functions. But of course there’s no function there - the calls go nowhere. At load time, when the operating system loads your EXE it also loads your DLL, and then it patches the EXE - where the EXE calls into the stub, the loader replaces that with a call into the real function in the DLL.

Normally you do not need to ship the .lib to your customers. However, if your customers want to write their own EXEs that use your DLL then you will need to send them the .lib so that they can link their EXE against it.

Linker error LNK1107 means that you’ve tried to link to the DLL rather than to the .lib. That’s always wrong, because by definition a DLL is linked dynamically at runtime, rather than statically at build time."

For Unreal Engine 4.16.0:

var basePath = Path.GetDirectoryName(RulesCompiler.GetFileNameFromType(GetType()));