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模块​​​​​​​

相关推荐
真的很上进1 分钟前
如何借助 Babel+TS+ESLint 构建现代 JS 工程环境?
java·前端·javascript·css·react.js·vue·html
web130933203987 分钟前
vue elementUI form组件动态添加el-form-item并且动态添加rules必填项校验方法
前端·vue.js·elementui
NiNg_1_23428 分钟前
Echarts连接数据库,实时绘制图表详解
前端·数据库·echarts
众拾达人33 分钟前
Android自动化测试实战 Java篇 主流工具 框架 脚本
android·java·开发语言
皓木.35 分钟前
Mybatis-Plus
java·开发语言
不良人天码星35 分钟前
lombok插件不生效
java·开发语言·intellij-idea
守护者1701 小时前
JAVA学习-练习试用Java实现“使用Arrays.toString方法将数组转换为字符串并打印出来”
java·学习
源码哥_博纳软云1 小时前
JAVA同城服务场馆门店预约系统支持H5小程序APP源码
java·开发语言·微信小程序·小程序·微信公众平台
禾高网络1 小时前
租赁小程序成品|租赁系统搭建核心功能
java·人工智能·小程序
学会沉淀。1 小时前
Docker学习
java·开发语言·学习