UE5 插件第三方库的build写法记录

作者 : admin 本文共4282个字,预计阅读时间需要11分钟 发布时间: 2024-06-9 共8人阅读
// Copyright Epic Games, Inc. All Rights Reserved.

using UnrealBuildTool;
using System.IO;

//方法为创建第三方库插件,CoustemOpenCV为插件名称
public class CoustemOpenCV : ModuleRules
{
	public CoustemOpenCV(ReadOnlyTargetRules Target) : base(Target)
	{

		PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;

		//ModuleDirectory = 创建插件生成的.Build.cs同级目录)
		//添加include
		PublicIncludePaths.AddRange(
	new string[] {
		Path.Combine(ModuleDirectory,"../ThirdParty/OpenCV_T/opencv/include")
		}
	);
		//添加Lib路径及*.lib文件名
		PublicAdditionalLibraries.AddRange(
	new string[] {
		Path.Combine(ModuleDirectory,"../ThirdParty/OpenCV_T/opencv/x64/vc15/lib","opencv_world3410.lib"),
	}
);
		//手动拷贝dll到项目Plugins\插件名称\Binaries\Win64内

		//可选是否添加if内容,添加后可省略手动拷贝dll的步骤
		if (Target.Platform == UnrealTargetPlatform.Win64)
		{
			string DLLPath = Path.Combine(ModuleDirectory, "ThirdParty/OpenCV_T/opencv/x64/vc15/bin", "opencv_world3410.dll");
			RuntimeDependencies.Add(DLLPath);

			// 确保在运行时复制 DLL 文件
			string ProjectBinariesDir = Path.Combine(Target.RelativeEnginePath, "Binaries", Target.Platform.ToString());
			RuntimeDependencies.Add(Path.Combine(ProjectBinariesDir, "opencv_world3410.dll"), StagedFileType.NonUFS);
		}


		PrivateIncludePaths.AddRange(
			new string[] {
				// ... add other private include paths required here ...
			}
			);
			
		
		PublicDependencyModuleNames.AddRange(
			new string[]
			{
				"Core",
				"CoustemOpenCVLibrary",
				"Projects"
				// ... add other public dependencies that you statically link with here ...
			}
			);
			
		
		PrivateDependencyModuleNames.AddRange(
			new string[]
			{
				// ... add private dependencies that you statically link with here ...	
			}
			);
		
		
		DynamicallyLoadedModuleNames.AddRange(
			new string[]
			{
				// ... add any modules that your module loads dynamically here ...
			}
			);
	}
}

这是另一种写法

using System.IO;
using System;
using UnrealBuildTool;



public class MP_opencv : ModuleRules
{
	public MP_opencv(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;
		
		// OpenCV include and library paths
		string OpenCVPath = Path.Combine(ModuleDirectory, "opencv");
		string OpenCVIncludePath = Path.Combine(OpenCVPath, "include");
		string OpenCVLibPath = Path.Combine(OpenCVPath, "x64");
		string OpenCVBinPath = Path.Combine(OpenCVPath, "bin");
		//Type = ModuleType.External;

		string includepats = Path.Combine(ModuleDirectory,"opencv\include");
		
		Console.WriteLine("ModuleDirectory::" + includepats);
		
		PublicIncludePaths.AddRange(
			new string[] {
				"MP_opencv/Public",
				OpenCVIncludePath // Ensure this path is correct
			}
		);
				
		// Add OpenCV library
		PublicAdditionalLibraries.Add(Path.Combine(OpenCVLibPath, "opencv_world410.lib"));

		// Add OpenCV runtime dependency (DLL)
		RuntimeDependencies.Add(Path.Combine(OpenCVBinPath, "opencv_world410.dll"));

模块方法:这个方法是创建thirdparty的文件夹,在项目的build.cs中添加。

// Copyright Epic Games, Inc. All Rights Reserved.

using System;
using System.IO;
using UnrealBuildTool;

public class MyOpCV : ModuleRules
{
	public bool LoadOpenCV(ReadOnlyTargetRules Target)
	{
		string MD_dir = ModuleDirectory;
		string thirdpartyRelative = Path.Combine(MD_dir, "../../ThirdParty/opencv");
		string thirdparty = Path.GetFullPath(thirdpartyRelative);
		string libpath = Path.Combine(thirdparty, "lib");
		string binpath = Path.Combine(thirdparty, "bin");

		Console.WriteLine("MD_dir: " + MD_dir);
		Console.WriteLine("thirdparty: " + thirdparty);
		Console.WriteLine("libpath: " + libpath);

		if (Target.Platform == UnrealTargetPlatform.Win64)
		{
			// 添加头文件路径
			PublicIncludePaths.Add(Path.Combine(thirdparty, "include"));
            
			// 添加库文件路径
			PublicSystemLibraryPaths.Add(libpath);
            
			// 添加额外的库文件
			PublicAdditionalLibraries.Add(Path.Combine(libpath, "opencv_world3410.lib"));

			// 添加运行时依赖项
			RuntimeDependencies.Add(Path.Combine(binpath, "opencv_world3410.dll"));

			return true;
		}

		return false;
	}

	public MyOpCV(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;

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

		// 调用加载 OpenCV 函数
		LoadOpenCV(Target);

		PrivateDependencyModuleNames.AddRange(new string[] { });
		//#include "PreOpenCVHeaders.h" // IWYU pragma: keep
		//#include "opencv2/calib3d.hpp"
		//#include "opencv2/imgproc.hpp"
		//#include "opencv2/imgcodecs.hpp"
		//#include "PostOpenCVHeaders.h" // IWYU pragma: keep
		
	}
}

UE5自带的opencv的打开方式

UE5 插件第三方库的build写法记录插图

#include “PreOpenCVHeaders.h” // IWYU pragma: keep
   #include “opencv2/calib3d.hpp”
    #include “opencv2/imgproc.hpp”
        #include “opencv2/imgcodecs.hpp”
 #include “PostOpenCVHeaders.h” // IWYU pragma: keep

官网opencv的加载方法

UE5 插件第三方库的build写法记录插图(1)

2.StartupMoudule函数UE5 插件第三方库的build写法记录插图(2)

如果没有写这里的话虽然可以编译过
但是运行的时候会提示丢失.dll
在delayhlp.cpp的delayLoadHelper2函数中的dli.pfnCur会报错.UE5 插件第三方库的build写法记录插图(3)

报错位置

UE5 插件第三方库的build写法记录插图(4)

只要是从官网上下的或者是原生编译没做特殊处理的OpenCV.
编译时这个文件都会报错.
大概有6条语法错误(大概可以手动改?)
然后我从别的工程里把没有报错的文件复制过来就能正常编译了.

UE5 插件第三方库的build写法记录插图(5)

不要在这种地方加OpenCV模块UE5 插件第三方库的build写法记录插图(6)​​​​​​​

本站无任何商业行为
个人在线分享 » UE5 插件第三方库的build写法记录
E-->