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

相关推荐
猿小蔡-Cool几秒前
CPU 多级缓存
java·spring·缓存
柏箱2 分钟前
使用JavaScript写一个网页端的四则运算器
前端·javascript·css
TU^3 分钟前
C语言习题~day16
c语言·前端·算法
gopher95115 分钟前
final,finally,finalize的区别
java·开发语言·jvm
Jason-河山13 分钟前
利用 Python 爬虫采集 1688商品详情
java·http
计算机源码社13 分钟前
分享一个餐饮连锁店点餐系统 餐馆食材采购系统Java、python、php三个版本(源码、调试、LW、开题、PPT)
java·python·php·毕业设计项目·计算机课程设计·计算机毕业设计源码·计算机毕业设计选题
Zww089117 分钟前
idea插件市场安装没反应
java·ide·intellij-idea
夜雨翦春韭19 分钟前
【代码随想录Day31】贪心算法Part05
java·数据结构·算法·leetcode·贪心算法
计算机学姐19 分钟前
基于微信小程序的调查问卷管理系统
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis