UE4: use of dynamic link library and static link library

Next, create a third person template C + + project ThirdPerson, using

UE4 learning notes: use of dynamic link library and static link library_ Much less confused blog - CSDN blog

1, Static link library

Create a new folder ThirdParty (the same level as the Content directory) in the project directory, copy the xxx.h file and xxx.lib file to this folder, and then add this directory to the include directory in the compiled file. Build.cs file, so that UE4 can search the header file (if the include directory is not added, the path of the header file is.. /.. / ThirdParty/xxx.h), Then add xxx.lib to the list of dependent libraries. The ThirdPerson.build.cs file is roughly as follows:

 
//ThirdPerson.Build.cs
using UnrealBuildTool;
using System.IO;
public class ThirdPerson : ModuleRules
{
    private string ThirdPartyPath
    {
        get { return Path.GetFullPath(Path.Combine(ModuleDirectory, "../../ThirdParty/")); }
    }
    public ThirdPerson(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
		PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "ImageWrapper" });
        PublicIncludePaths.Add(Path.Combine(ThirdPartyPath));
        PublicAdditionalLibraries.Add(Path.Combine(ThirdPartyPath, "xxx.lib"));
		PublicDelayLoadDLLs.Add("xxx.dll");
    }
}

2, Dynamic link library

At the same time, the dynamic link library needs to put the xxx.dll file into the ThirdParty directory. There are two ways to use the dynamic link library: one is to call directly through the. h and. lib files, and the other is to use only the DLL file by looking up the function address.

(1) Using dynamic link libraries through. lib files

The contents of the ThirdPerson.build.cs file are similar to those above, except that the dll file should be added to the delayed loading list PublicDelayLoadDLLs. If it is not added, it will be loaded when the engine starts, resulting in an error (I don't know if there are other solutions).

 
//ThirdPerson.Build.cs
using UnrealBuildTool;
using System.IO;
public class ThirdPerson : ModuleRules
{
    private string ThirdPartyPath
    {
        get { return Path.GetFullPath(Path.Combine(ModuleDirectory, "../../ThirdParty/")); }
    }
    public ThirdPerson(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
		PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "ImageWrapper" });
        PublicIncludePaths.Add(Path.Combine(ThirdPartyPath));
        PublicAdditionalLibraries.Add(Path.Combine(ThirdPartyPath, "xxx.lib"));
		PublicDelayLoadDLLs.Add("xxx.dll");
    }
}

Before using the functions in the dll, add the Thirdparty directory to the dll search path by calling the PushDllDirectory function, otherwise the dll file cannot be found (it can not be called if the dll file is placed in the project directory / Binaries\Win64).

If PushDllDirectory is a reference to a module, add it in StartupModule() or overload

void FKxFramework::StartupModule()
{
	FPlatformProcess::PushDllDirectory(*(FPaths::ProjectDir() / TEXT("Source/ThirdParty/xxx/")));
}

Or add in GameMode

void AThirdPersonGameMode::StartPlay()
{
	Super::StartPlay();
	FPlatformProcess::PushDllDirectory(*(FPaths::ProjectDir() / TEXT("ThirdParty/")));//Add dll storage directory to search path
}

(2) Use dll files directly

Directly using dll file to call the functions in the library only requires a dll file, and the. build.cs file does not need to be changed. You can directly use GetDllExport to obtain the functions to be called from the dll through the function name.

void AThirdPersonGameMode::StartPlay()
{
	Super::StartPlay();
	FPlatformProcess::PushDllDirectory(*(FPaths::ProjectDir() / TEXT("ThirdParty/")));//Add dll storage directory to search path
	CaptureScreenHandle = FPlatformProcess::GetDllHandle(TEXT("xxx.dll"));//Or full path FPaths::ProjectDir()/TEXT("ThirdParty/xxx.dll")
	}

but !

If the dll file does not depend on other dll files, GetDllHandle can pass in the full path of the dll file without PushDllDirectory. However, if the dll also depends on other dll files and is also in the Thirdparty directory, PushDllDirectory must be called, otherwise the dependent dll file cannot be found.

Tags: UE4

Posted on Sat, 18 Sep 2021 10:54:45 -0400 by rhodry_korb