UE4【C++】windows平台使用第三方库【一】

一,内容概述

如果想用第三方库,不得不提一下UE4的编译系统,UnrealBuildTool。

UE4项目都是以模块为单位,不同模块构成整个系统。

UnrealBuildTool会将不同模块编译到一起,每一个module都需要一个.build.cs文件,当然这个文件还做其他很多工作,比如跨平台等。

使用第三方库的两种方法:

1,直接在工程项目的build.cs文件中配置这个第三方库。

2,根据UE4的模块定义,新构建一个模块,每一个模块都要有一个build.cs,通过build.cs对第三方库配置路径及库名,在项目中的buil.cs导入这个module。

(备注,本文只介绍静态库,与动态库有区别的,静态库在编译、期就要连接到工程dll中,所以要修改build.cs。

而第三方的动态库是在运行时再加载使用,所以一般不去更改build.cs,但要在cpp文件去显示调用。)

本文先介绍第一种方法,第二种后续再补上。

二,静态库的引用:创建与使用流程

1,在VS中,点击新建项目——Visual C++——Win32项目(比如名称为DllTest,实现一个加法,add)。

点击确定后,在导航窗口中选择静态库。

添加自己的类代码,修改为x64平台并生成DllTest.lib文件。(Debug与Release都可以)

(一般的非虚幻项目中,引用外部库只需要设置,项目->属性->配置属性->VC++目录,添加包含目录,库目录,ok.

代码中载入库文件 #pragma comment(lib," DllTest.lib ");就可以了,然而像前面提到的,虚幻有自己的编译系统,这么使用可以运行,但是无法打包

2,新建一个UE4项目工程A.uproject,在期工程目录下建立一个层级文件夹,分别命名为ThirdParty-MyCalculation-inluce/lib,如下图

2,打开项目build.cs文件进行编辑,在\SNVRClayShooting\Source\SNVRClayShooting\目录下。

 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 using System.IO;
 4 using UnrealBuildTool;
 5 
 6 public class SNVRClayShooting : ModuleRules
 7 {
 8     public SNVRClayShooting(ReadOnlyTargetRules Target) : base(Target)
 9     {
10         PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
11     
12         PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "ApexDestruction" });
13 
14         PrivateDependencyModuleNames.AddRange(new string[] { "SteamVR" });
15 
16         PrivateDependencyModuleNames.AddRange(new string[] { "NewModule" });
17 
18         //AddEngineThirdPartyPrivateStaticDependencies(Target, "MySQLSupport");
19 
20         // Uncomment if you are using Slate UI
21         // PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
22 
23         // Uncomment if you are using online features
24         // PrivateDependencyModuleNames.Add("OnlineSubsystem");
25 
26         // To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
27 
28         //         //string ModulePath = Path.GetDirectoryName(r.GetModuleFileName(this.GetType().Name).CanonicalName);
29         //         string ModulePath = ModuleDirectory;
30         //         // gets the directory path of this module        
31         //         string ThirdPartyPath = Path.GetFullPath(Path.Combine(ModulePath, "../../ThirdParty/"));
32         //         // gets the ThirdParty folder directory path
33         //         string MySQLConnectorPath = ThirdPartyPath + "MySQLConnector.C6.1/";
34         //         // gets the MySQL Connector.C 6.1 folder path
35         //         string MySQLConnectorLibraryPath = MySQLConnectorPath + "lib/";
36         //         // gets the path of the lib folder      
37         //         string MySQLConnectorIncludePath = MySQLConnectorPath + "include/";
38         //         // gets the path of the include folder      
39         //         string MySQLConnectorImportLibraryName = Path.Combine(MySQLConnectorLibraryPath, "libmysql.lib");
40         //         // gets the file path and name of the libmysql.lib static import library 
41         //         string MySQLConnectorDLLName = Path.Combine(MySQLConnectorLibraryPath, "libmysql.dll");
42         //         // gets the file path and name of libmysql.dll    
43         //         if (!File.Exists(MySQLConnectorImportLibraryName))
44         //         // check to ensure the static import lib can be located, or else we'll be in trouble        
45         //         {
46         //             throw new BuildException(string.Format("{0} could not be found.", MySQLConnectorImportLibraryName));
47         //             // log an error message explaining what went wrong if not found       
48         //         }
49         //         if (!File.Exists(MySQLConnectorDLLName))
50         //         // check to make sure the dll can be located or else we'll be in trouble        
51         //         {
52         //             throw new BuildException(string.Format("{0} could not be found.", MySQLConnectorDLLName));
53         //             // log an error message explaining what went wrong if not found       
54         //         }
55         //         PrivateIncludePaths.Add(MySQLConnectorIncludePath);
56         //         // add the "include" folder to our dependencies. I've chosen PrivateIncludePaths since I hide the mysql headers from external code 
57         //         PublicLibraryPaths.Add(MySQLConnectorLibraryPath);
58         //         // add the "lib" folder to our dependencies        
59         //         PublicAdditionalLibraries.Add(MySQLConnectorImportLibraryName);
60         //         // add libmysql.lib static import library as a dependency       
61         //         PublicDelayLoadDLLs.Add(MySQLConnectorDLLName);
62         //         // add libmysql.dll as a dll    
63         
64         string ModulePath = ModuleDirectory;
65         // gets the directory path of this module        
66         string ThirdPartyPath = Path.GetFullPath(Path.Combine(ModulePath, "../../ThirdParty/"));
67         // gets the ThirdParty folder directory path
68         string MySQLConnectorPath = ThirdPartyPath + "MyCalculation/";
69         // gets the MySQL Connector.C 6.1 folder path
70         string MySQLConnectorLibraryPath = MySQLConnectorPath + "lib/Win64/";
71         // gets the path of the lib folder      
72         string MySQLConnectorIncludePath = MySQLConnectorPath + "include/";
73         // gets the path of the include folder      
74         string MySQLConnectorImportLibraryName = Path.Combine(MySQLConnectorLibraryPath, "DllTest.lib");
75         // gets the file path and name of the libmysql.lib static import library 
76   
77         if (!File.Exists(MySQLConnectorImportLibraryName))
78         // check to ensure the static import lib can be located, or else we'll be in trouble        
79         {
80             throw new BuildException(string.Format("{0} could not be found.", MySQLConnectorImportLibraryName));
81             // log an error message explaining what went wrong if not found       
82         }
83 
84         PrivateIncludePaths.Add(MySQLConnectorIncludePath);
85         // add the "include" folder to our dependencies. I've chosen PrivateIncludePaths since I hide the mysql headers from external code 
86         PublicLibraryPaths.Add(MySQLConnectorLibraryPath);
87         // add the "lib" folder to our dependencies        
88         PublicAdditionalLibraries.Add(MySQLConnectorImportLibraryName);
89         // add libmysql.lib static import library as a dependency       
90     }
91 }
View Code

build.cs中就是把第三方静态库路径及名称配置好,跟VS的差别就是VS是配置在VS工具的属性里,UE4需要配置在编译系统的UnrealBuildTool里。

3,在项目代码中#include 第三方库所需要的头文件就可以正常使用。

以上亲测通过,包括打包也没有问题。

猜你喜欢

转载自www.cnblogs.com/yyfamily/p/9540447.html