首页 > 编程语言 >【UE4 C++】调用外部链接库:lib静态库

【UE4 C++】调用外部链接库:lib静态库

时间:2023-01-25 21:13:46浏览次数:41  
标签:... return string lib 链接库 C++ Path include

本例以插件形式测试

  • 使用Lib引用,打包程序运行不用再拷贝lib文件
  • 需要 lib 文件和 .h 头文件

lib部分的代码

  • .h 头文件
    1.   #pragma once
    2.   #ifndef __MYTEST_LIB_H__
    3.   #define __MYTEST_LIB_H__
    4.   #include <string>
    5.   #include <iostream>
    6.    
    7.   int myPrint( int _age);
    8.    
    9.   #endif
  • .cpp 文件
    1.   #include "MyTestLib.h"
    2.    
    3.   int myPrint(int _age)
    4.   {
    5.   return _age + 1000;
    6.   }

UE4 插件代码

Plugin lib文件部署

插件 build.cs设置

  1.   using System.IO;
  2.   namespace UnrealBuildTool.Rules
  3.   {
  4.   public class JsonPlugin : ModuleRules
  5.   {
  6.   private string ModulePath
  7.   {
  8.   // get { return Path.GetDirectoryName(RulesCompiler.GetModuleFilename(this.GetType().Name)); }
  9.   get { return ModuleDirectory; }
  10.   }
  11.    
  12.   private string ThirdPartyPath
  13.   {
  14.   get { return Path.GetFullPath(Path.Combine(ModulePath, "../../ThirdParty/")); }
  15.   }
  16.   private string MyLibPath //第三方库MyTestLib的目录
  17.   {
  18.   get { return Path.GetFullPath(Path.Combine(ThirdPartyPath, "mylib")); }
  19.   }
  20.    
  21.   public JsonPlugin(TargetInfo Target)
  22.   {
  23.   PublicIncludePaths.AddRange(
  24.   new string[] {
  25.   "JsonPlugin/Public",
  26.   // ... add public include paths required here ...
  27.   }
  28.   );
  29.    
  30.   PrivateIncludePaths.AddRange(
  31.   new string[] {
  32.   "JsonPlugin/Private",
  33.   // ... add other private include paths required here ...
  34.   }
  35.   );
  36.    
  37.   PublicDependencyModuleNames.AddRange(
  38.   new string[]
  39.   {
  40.   "Core",
  41.   "CoreUObject",
  42.   "Engine",
  43.   "HTTP",
  44.   "Json"
  45.   // ... add other public dependencies that you statically link with here ...
  46.   }
  47.   );
  48.    
  49.   PrivateDependencyModuleNames.AddRange(
  50.   new string[]
  51.   {
  52.   // ... add private dependencies that you statically link with here ...
  53.   }
  54.   );
  55.    
  56.   DynamicallyLoadedModuleNames.AddRange(
  57.   new string[]
  58.   {
  59.   // ... add any modules that your module loads dynamically here ...
  60.   }
  61.   );
  62.    
  63.   LoadThirdPartyLib(Target);
  64.   }
  65.    
  66.   public bool LoadThirdPartyLib(TargetInfo Target)
  67.   {
  68.   bool isLibrarySupported = false;
  69.   if ((Target.Platform == UnrealTargetPlatform.Win64) || (Target.Platform == UnrealTargetPlatform.Win32))//平台判断
  70.   {
  71.   isLibrarySupported = true;
  72.   System.Console.WriteLine("----- isLibrarySupported true");
  73.   //string PlatformSubPath = (Target.Platform == UnrealTargetPlatform.Win64) ? "Win64" : "Win32";
  74.   string LibrariesPath = Path.Combine(MyLibPath, "Lib");
  75.   PublicAdditionalLibraries.Add(Path.Combine(LibrariesPath,/* PlatformSubPath,*/ "MyTestLib.lib"));//加载第三方静态库.lib
  76.   }
  77.    
  78.   if (isLibrarySupported) //成功加载库的情况下,包含第三方库的头文件
  79.   {
  80.   // Include path
  81.   System.Console.WriteLine("----- PublicIncludePaths.Add true");
  82.   PublicIncludePaths.Add(Path.Combine(MyLibPath, "Include"));
  83.   }
  84.   return isLibrarySupported;
  85.   }
  86.   }
  87.   }

调用

  • JsonFunction.cpp代码
  1.   #include "../ThirdParty/mylib/Include/MyTestLib.h"
  2.    
  3.   int UJsonFunction::MyOutput()
  4.   {
  5.   int str = myPrint(100); //lib 里的函数
  6.   return str;
  7.   }

 

标签:...,return,string,lib,链接库,C++,Path,include
From: https://www.cnblogs.com/yang131/p/17067283.html

相关文章

  • 基于EasyX和Raylib的自由落体小球
    这个简陋的小游戏,在《C和C++游戏趣味编程》第三章,是逐次迭代写成的。这里贴出基于easyx和raylib的各自实现。基于EasyX//根据《C和C++游戏趣味编程》第二章仿......
  • 基于EasyX和Raylib的十字消除
    基于EasyX//根据《C和C++游戏趣味编程》第10章十字消除写出#include<graphics.h>#include<conio.h>//_kbhit()#include<stdio.h>#include<stdlib.h>#inc......
  • 基于EasyX和Raylib的别碰方块
    基于EasyX//根据《C和C++游戏趣味编程》第三章别碰方块写出#include<graphics.h>#include<conio.h>//_kbhit()#include<stdio.h>//检测按下了空格键voi......
  • 基于EasyX和Raylib的推箱子
    基于EasyX//根据《C和C++游戏趣味编程》第九章推箱子写出#include<graphics.h>#include<conio.h>//_kbhit()#include<stdio.h>#include<stdlib.h>//玩......
  • 基于EasyX和Raylib的坚持100秒
    EasyX//根据《C和C++游戏趣味编程》第12章坚持100秒写出#include<graphics.h>#include<conio.h>//_kbhit()#include<stdio.h>#include<stdlib.h>#include......
  • 基于EasyX和Raylib的字符雨
    思路按如下顺序尝试:绘制一个字符下落绘制4个字符(一列)的下落绘制20个字符(一列)的下落,并封装其位置更新、绘制的过程为Rain类的成员函数绘制多个雨滴每个雨滴在更新......
  • 基于EasyX和Raylib的贪吃蛇
    基于EasyX//根据《C和C++游戏趣味编程》第七章贪吃蛇写出#include<graphics.h>#include<conio.h>//_kbhit()#include<stdio.h>#include<stdlib.h>//全......
  • 详细实例说明+典型案例实现 对枚举法进行全面分析 | C++
    第五章枚举法:::hljs-center目录第五章枚举法●前言1.简要介绍2.代码及结果示例(简单理解)3.生活实例●二、枚举法的典型案例——鸡兔同笼&质数求解1.鸡......
  • C++Day09 深拷贝、写时复制(cow)、短字符串优化
    一、std::string的底层实现1、深拷贝1classString{2public:3String(constString&rhs):m_pstr(newchar[strlen(rhs)+1]()){4}5private:6cha......
  • 【转】c++中Vector等STL容器的自定义排序
    三种方式实现vector的自定义排序方法1:重载运算符#include<vector>#include<algorithm>#include<functional>usingnamespacestd;structTItem{intm_i......