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

相关推荐
栈老师不回家14 分钟前
Vue 计算属性和监听器
前端·javascript·vue.js
WaaTong17 分钟前
《重学Java设计模式》之 原型模式
java·设计模式·原型模式
m0_7430484417 分钟前
初识Java EE和Spring Boot
java·java-ee
AskHarries19 分钟前
Java字节码增强库ByteBuddy
java·后端
前端啊龙20 分钟前
用vue3封装丶高仿element-plus里面的日期联级选择器,日期选择器
前端·javascript·vue.js
一颗松鼠24 分钟前
JavaScript 闭包是什么?简单到看完就理解!
开发语言·前端·javascript·ecmascript
小灰灰__39 分钟前
IDEA加载通义灵码插件及使用指南
java·ide·intellij-idea
夜雨翦春韭42 分钟前
Java中的动态代理
java·开发语言·aop·动态代理
小远yyds44 分钟前
前端Web用户 token 持久化
开发语言·前端·javascript·vue.js
程序媛小果1 小时前
基于java+SpringBoot+Vue的宠物咖啡馆平台设计与实现
java·vue.js·spring boot