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

cs 复制代码
// 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 ...
			}
			);
	}
}

这是另一种写法

cs 复制代码
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中添加。

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的打开方式

#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的加载方法

2.StartupMoudule函数

如果没有写这里的话虽然可以编译过

但是运行的时候会提示丢失.dll

在delayhlp.cpp的delayLoadHelper2函数中的dli.pfnCur会报错.

报错位置

只要是从官网上下的或者是原生编译没做特殊处理的OpenCV.

编译时这个文件都会报错.

大概有6条语法错误(大概可以手动改?)

然后我从别的工程里把没有报错的文件复制过来就能正常编译了.

不要在这种地方加OpenCV模块​​​​​​​

相关推荐
Mapmost7 分钟前
【高斯泼溅】从一张好照片开始:Mapmost 3DGS建模之图像采集指南
前端
大隐隐于野15 分钟前
从零开始理解和编写LLM中的KV缓存
java·缓存·llm
李少兄17 分钟前
解决 Chrome 下载 `.crx` 文件被自动删除及“无法安装扩展程序,因为它使用了不受支持的清单版本”问题
前端·chrome
孟祥_成都23 分钟前
最好的组件库教程又回来了,升级为 headless 组件库!
前端·面试·架构
DKunYu33 分钟前
1.多线程初阶
java·开发语言
Man39 分钟前
当我们执行 npm run xxx 的时候实际执行逻辑和流程
前端·javascript·前端框架
尤利乌斯.X40 分钟前
在Java中调用MATLAB函数的完整流程:从打包-jar-到服务器部署
java·服务器·python·matlab·ci/cd·jar·个人开发
spencer_tseng1 小时前
easy-captcha-1.6.2.jar
java·jar
曾富贵1 小时前
【eslint】快速配置
前端
阿珊和她的猫1 小时前
Webpack Loader 和 Plugin 实现原理详解
前端·webpack·node.js