How to modify build file to copy DLLs to binaries?

This is how I do it. Each Third Party lib uses a Build.cs file and the module is added as dependency in the main project’s Build.cs file.

Here is how the hypothetical module “YourLib” works.

YourLib.Build.cs:

using System;
using System.IO;
using UnrealBuildTool;

public class YourLib : ModuleRules
{
    public string GetUProjectPath()
    {
		//Change this according to your module's relative location to your project file. If there is any better way to do this I'm interested!
		//Assuming Source/ThirdParty/YourLib/
        return Directory.GetParent(ModuleDirectory).Parent.Parent.ToString();
    }

    private void CopyToBinaries(string Filepath, TargetInfo Target)
    {
        string binariesDir = Path.Combine(GetUProjectPath(), "Binaries", Target.Platform.ToString());
        string filename = Path.GetFileName(Filepath);

        if (!Directory.Exists(binariesDir))
            Directory.CreateDirectory(binariesDir);

        if (!File.Exists(Path.Combine(binariesDir, filename)))
            File.Copy(Filepath, Path.Combine(binariesDir, filename), true);
    }

    public YourLib(TargetInfo Target)
	{
		Type = ModuleType.External;

		Definitions.Add("WITH_YOURLIB=1");

		// Compile and link with YOURLIB

		PublicIncludePaths.Add(ModuleDirectory + "/include");
		PublicAdditionalLibraries.Add(ModuleDirectory + "/lib/YourLib.lib");
		CopyToBinaries(ModuleDirectory + "/lib/YourLib.dll", Target);
	}
}

Hope this helps!

2 Likes