Keywords: UE4, Build.cs

How to get the current project directory

New way

using System.IO;
using UnrealBuildTool;  

public class MyProject : ModuleRules  
{  
    private string ProjectRoot
    {
        get
        {
            return Path.GetFullPath(Path.Combine(ModuleDirectory, "../../"));
        }
    }
	
    private string ThirdPartyPath
    { 
        get 
        { 
            return Path.GetFullPath(Path.Combine(ModuleDirectory, ProjectRoot, "ThirdParty")); 
        } 
    }

    public MyProject(ReadOnlyTargetRules Target) : base(Target)
    {  
	
    }  
}

Reference:
https://answers.unrealengine.com/questions/237166/how-can-i-get-the-current-project-path-within-the.html

Old way (Removed in 4.21)

using System.IO;  
using UnrealBuildTool;  
  
public class MyProject : ModuleRules  
{  
    RulesAssembly r;  
  
    private string ModulePath  
    {  
        get { return Path.GetDirectoryName( r.GetModuleFileName(this.GetType().Name).CanonicalName); }  
    }  
  
    private string ThirdPartyPath  
    {  
        get { return Path.GetFullPath(Path.Combine(ModulePath, "../../ThirdParty/")); }  
    }  
  
    public MyProject(TargetInfo Target)  
    {  
  
        FileReference CheckProjectFile;  
        UProjectInfo.TryGetProjectForTarget("MyProject", out CheckProjectFile);  
        r = RulesCompiler.CreateProjectRulesAssembly(CheckProjectFile);  
    }  
}
How to include third party libraries

Example:

using UnrealBuildTool;
using System.IO;

public class TestTD : ModuleRules
{
    private string ProjectRoot
    {
        get
        {
            return Path.GetFullPath(Path.Combine(ModuleDirectory, "../../"));
        }
    }
	
    private string ThirdPartyPath
    { 
        get 
        { 
            return Path.GetFullPath(Path.Combine(ModuleDirectory, ProjectRoot, "ThirdParty")); 
        } 
    }
	
    public TestTD(ReadOnlyTargetRules Target) : base(Target)
    {
        PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;

        PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "NavigationSystem", "AIModule" });

        LoadOpenCV(Target);
    }
	
    public bool LoadOpenCV(ReadOnlyTargetRules Target)
    {	
        // Start OpenCV linking here!
        bool isLibrarySupported = false;

        //Library path
        string PlatformString = null;
        string libSuffix = null;
        switch (Target.Platform)
        {
            case UnrealTargetPlatform.Win64:
                {
                    PlatformString = "Win64";
                    libSuffix = ".lib";
                    break;
                }
            case UnrealTargetPlatform.Android:
                {
                    PlatformString = "Android";
                    libSuffix = ".a";
                    break;
                }
        }
		
        bool isdebug = Target.Configuration == UnrealTargetConfiguration.Debug;
        string DebugLibNameFlag = "";
        if(isdebug)
        {
            DebugLibNameFlag = "d";
        }

        if (null != PlatformString)
        {
            //LibrariesPath value for Windows in Debug: D:\MyProj\ThirdParty\OpenCV\Libraries\Win64\opencv_world348d.lib
            string LibrariesPath = Path.Combine(ThirdPartyPath, "OpenCV", "Libraries", PlatformString, "opencv_world348" + DebugLibNameFlag + libSuffix);
            PublicAdditionalLibraries.Add(LibrariesPath);
			
            System.Console.WriteLine("++++++++++++ Set OpenCV Libraries: " + LibrariesPath + "\r");
        }

        //Include path
        string IncludePath = Path.Combine(ThirdPartyPath, "OpenCV", "Includes");
        PublicIncludePaths.Add(IncludePath);

        System.Console.WriteLine("++++++++++++ Set OpenCV Includes: " + IncludePath + "\r");
		
        PublicDefinitions.Add(string.Format("WITH_OPENCV_BINDING={0}", isLibrarySupported ? 1 : 0));

        return isLibrarySupported;
    }
}

If want to include a number of libraries, you can do like this:

// Add static library search path 
PublicLibraryPaths.Add(LibPath);

PublicAdditionalLibraries.Add("opencv_calib3d340.lib");
PublicAdditionalLibraries.Add("opencv_core340.lib");
PublicAdditionalLibraries.Add("opencv_cudaarithm340.lib");
PublicAdditionalLibraries.Add("opencv_cudabgsegm340.lib");

// Add shared library search path
PublicRuntimeLibraryPaths.Add(DllPath);

PublicDelayLoadDLLs.Add("opencv_calib3d340.dll");
PublicDelayLoadDLLs.Add("opencv_core340.dll");
PublicDelayLoadDLLs.Add("opencv_cudaarithm340.dll");
PublicDelayLoadDLLs.Add("opencv_cudabgsegm340.dll");

Reference:
[UE4]UnrealBuildTool APIs in common
https://dawnarc.com/2019/07/ue4unrealbuildtool-apis-in-common/


我年轻时,听一位教授讲:“人生都只有一个想法,终其一生不过是不断丰富它。”我当时觉得:“一辈子只围绕一个念头转?这未免也太反动了!”五十岁时,我明白他是对的:我这一辈子都在追随一个想法,问题是我从来都不知道是哪个。──翁贝托·埃科《密涅瓦火柴盒》