【UE4 C++】调用外部链接库:lib静态库
简述
- 本例以插件形式测试
- 使用Lib引用,打包程序运行不用再拷贝lib文件
- 需要 lib 文件和 .h 头文件
lib部分的代码
- .h 头文件
#pragma once #ifndef __MYTEST_LIB_H__ #define __MYTEST_LIB_H__ #include <string> #include <iostream> int myPrint( int _age); #endif
- .cpp 文件
#include "MyTestLib.h" int myPrint(int _age) { return _age + 1000; }
UE4 插件代码
Plugin lib文件部署
插件 build.cs设置
using System.IO;
namespace UnrealBuildTool.Rules
{
public class JsonPlugin : ModuleRules
{
private string ModulePath
{
// get { return Path.GetDirectoryName(RulesCompiler.GetModuleFilename(this.GetType().Name)); }
get { return ModuleDirectory; }
}
private string ThirdPartyPath
{
get { return Path.GetFullPath(Path.Combine(ModulePath, "../../ThirdParty/")); }
}
private string MyLibPath //第三方库MyTestLib的目录
{
get { return Path.GetFullPath(Path.Combine(ThirdPartyPath, "mylib")); }
}
public JsonPlugin(TargetInfo Target)
{
PublicIncludePaths.AddRange(
new string[] {
"JsonPlugin/Public",
// ... add public include paths required here ...
}
);
PrivateIncludePaths.AddRange(
new string[] {
"JsonPlugin/Private",
// ... add other private include paths required here ...
}
);
PublicDependencyModuleNames.AddRange(
new string[]
{
"Core",
"CoreUObject",
"Engine",
"HTTP",
"Json"
// ... 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 ...
}
);
LoadThirdPartyLib(Target);
}
public bool LoadThirdPartyLib(TargetInfo Target)
{
bool isLibrarySupported = false;
if ((Target.Platform == UnrealTargetPlatform.Win64) || (Target.Platform == UnrealTargetPlatform.Win32))//平台判断
{
isLibrarySupported = true;
System.Console.WriteLine("----- isLibrarySupported true");
//string PlatformSubPath = (Target.Platform == UnrealTargetPlatform.Win64) ? "Win64" : "Win32";
string LibrariesPath = Path.Combine(MyLibPath, "Lib");
PublicAdditionalLibraries.Add(Path.Combine(LibrariesPath,/* PlatformSubPath,*/ "MyTestLib.lib"));//加载第三方静态库.lib
}
if (isLibrarySupported) //成功加载库的情况下,包含第三方库的头文件
{
// Include path
System.Console.WriteLine("----- PublicIncludePaths.Add true");
PublicIncludePaths.Add(Path.Combine(MyLibPath, "Include"));
}
return isLibrarySupported;
}
}
}
调用
- JsonFunction.cpp代码
#include "../ThirdParty/mylib/Include/MyTestLib.h"
int UJsonFunction::MyOutput()
{
int str = myPrint(100); //lib 里的函数
return str;
}
原文最早发布于:https://blog.csdn.net/Szu_IT_Man/article/details/58232638
作者:砥才人
出处:https://www.cnblogs.com/shiroe
本系列文章为笔者整理原创,只发表在博客园上,欢迎分享本文链接,如需转载,请注明出处!