UE4【C++】windows平台使⽤第三⽅库【⼀】
⼀,内容概述
如果想⽤第三⽅库,不得不提⼀下UE4的编译系统,UnrealBuildTool。
UE4项⽬都是以模块为单位,不同模块构成整个系统。
UnrealBuildTool会将不同模块编译到⼀起,每⼀个module都需要⼀个.build.cs⽂件,当然这个⽂件还做其他很多⼯作,⽐如跨平台等。
使⽤第三⽅库的两种⽅法:
1,直接在⼯程项⽬的build.cs⽂件中配置这个第三⽅库。
2,根据UE4的模块定义,新构建⼀个模块,每⼀个模块都要有⼀个build.cs,通过build.cs对第三⽅库配置路径及库名,在项⽬中的
buil.cs导⼊这个module。
(备注,本⽂只介绍静态库,与动态库有区别的,静态库在编译、期就要连接到⼯程dll中,所以要修改b
uild.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
3using System.IO;
4using UnrealBuildTool;
5
6public class SNVRClayShooting : ModuleRules
7 {
8public 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
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
mysql下载的vs库放在那个文件里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
64string ModulePath = ModuleDirectory;
65// gets the directory path of this module
66string ThirdPartyPath = Path.GetFullPath(Path.Combine(ModulePath, "../../ThirdParty/"));
67// gets the ThirdParty folder directory path
68string MySQLConnectorPath = ThirdPartyPath + "MyCalculation/";
69// gets the MySQL Connector.C 6.1 folder path
70string MySQLConnectorLibraryPath = MySQLConnectorPath + "lib/Win64/";
71// gets the path of the lib folder
72string MySQLConnectorIncludePath = MySQLConnectorPath + "include/";
73// gets the path of the include folder
74string MySQLConnectorImportLibraryName = Path.Combine(MySQLConnectorLibraryPath, "DllTest.lib");
75// gets the file path and name of the libmysql.lib static import library
76
77if (!File.Exists(MySQLConnectorImportLibraryName))
78// check to ensure the static import lib can be located, or else we'll be in trouble
79 {
80throw 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 第三⽅库所需要的头⽂件就可以正常使⽤。
以上亲测通过,包括打包也没有问题。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论