百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术教程 > 正文

Unreal|ue中使用lib和dll(ue4 could not be compiled)

csdh11 2025-03-14 15:57 16 浏览

下面会展示如何创建一个库或dll文件,并提供一些为这个分享创建的实例文件。


1

创建lib文件





为了在 Visual Studio 中创建 lib 文件,请搜索表达式“Static Libray”并创建一个新项目


在创建的示例项目中,添加了一个新的头文件和源文件,分别名为 OrfeasMathLibrary.h 和 OrfeasMathLibrary.cpp。下面是具体的代码:

//OrfeasMathLibrary.h
namespace OrfeasMathLibrary
{
  class Arithmetic
  {
  public:


    // Returns a + b
    static double Add(double a, double b);


    // Returns a - b
    static double Subtract(double a, double b);


    // Returns a * b
    static double Multiply(double a, double b);


    // Returns a / b
    static double Divide(double a, double b);
  };
}


// OrfeasStaticLibrary.cpp : Defines the functions for the static library.
#include "pch.h"
#include "OrfeasMathLibrary.h"


namespace OrfeasMathLibrary
{
  double Arithmetic::Add(double a, double b)
  {
    return a + b;
  }


  double Arithmetic::Subtract(double a, double b)
  {
    return a - b;
  }


  double Arithmetic::Multiply(double a, double b)
  {
    return a * b;
  }


  double Arithmetic::Divide(double a, double b)
  {
    return a / b;
  }
}

pch头文件是Visual Studio生成的预编译头。因此,一旦输入了上面的代码,就可以在Release 和 x64 版本中编译项目。理想情况下,应该在构建 ue4 项目的所有不同场景中编译项目,以保持兼容性。这意味着可能还包括调试版本和 x32 版本。

在 x64 版本中编译项目后,将在 Visual Studio 的项目目录中生成以下文件:[
Visual_Studio_Project_Directory]->x64->Release->NameOfSolution.lib

请记住该文件的位置,因为我们需要在下一步中在 ue4 项目中引用


2

使用.lib文件

为了使用我们上面创建的 lib 文件,首先,导航到 .Build.cs 文件中的 ue4 项目并添加以下行:

//Change this to match your lib file's path
PublicAdditionalLibraries.Add(@"C:/Users/Orfeas/source/repos/OrfeasStaticLibrary/x64/Release/OrfeasStaticLibrary.lib"); 

然后,为了从 .lib 文件调用“Add”函数,创建一个新的蓝图函数库并在头文件中键入以下代码:

public:


  UFUNCTION(BlueprintCallable)
  static void PrintSumFromLib(float a, float b);

然后,在其源文件中包含头文件“OrfeasMathLibrary.h”(或确保与 lib 文件中的 C++ 类的名称匹配)。

然后,输入 PrintSumFromLib 函数的以下实现:

void UOrfeasBlueprintFunctionLibrary::PrintSumFromLib(float a, float b)
{
  //At this point make sure to
  //1) Include the "OrfeasMathLibrary.h"
  //2) Include the lib file complete path in PublicAdditionalLibraries in your [Project].Build.cs file
  double Sum = OrfeasMathLibrary::Arithmetic::Add(a, b);
  GLog->Log("Sum from static lib:" + FString::SanitizeFloat(Sum));
}  

此时,你可以从蓝图中的任何位置调用该函数并查看来自 .lib 文件的结果。此外,在蓝图里面的自动识别应该能够识别这些功能并提供有用的提示。

3

创建dll文件


为了创建 dll 文件,需要从 Visual Studio 选择带有导出的动态链接库或动态链接库。


如果选择第一个,Visual Studio 将生成一些模板代码。你可以在此基础上构建或输入你自己的代码。

这里我的项目的头文件:

// ---- Code generated by VS
// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the MATHISFUNDLL_EXPORTS
// symbol defined on the command line. This symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// MATHISFUNDLL_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef MATHISFUNDLL_EXPORTS
#define MATHISFUNDLL_API __declspec(dllexport)
#else
#define MATHISFUNDLL_API __declspec(dllimport)
#endif
// ---- end of code generated by VS


//By default C++ is performing name mangling of types.
//This means that if we have a function named "Sum" the compiler may internally generate a different name for it eg __Sum_ or something different
//By including the following ifdef we're telling the compiler "In case you're compiling C++ language, make sure the method names have C linkage (ie don't change their names)
//For more information: https://stackoverflow.com/questions/1041866/what-is-the-effect-of-extern-c-in-c
#ifdef __cplusplus    
extern "C"
{
#endif


MATHISFUNDLL_API float Sum(float a, float b);
MATHISFUNDLL_API int GetFibonacciNTerm(int Term);
#ifdef __cplusplus
}
#endif

我们在开始函数之前键入 MATHISFUNDLL_API 的原因是将它们标记为 __declspec(dllexport),这意味着我们希望使它们对使用我们的 dll 的应用程序可见。有关使用 extern C 的更多信息,请查看上面标注的评论并点击提供的链接。

现在我们已经完成了头文件,所以让我们继续输入上面提到的函数:

MATHISFUNDLL_API float Sum(float a, float b)
{
    return a+b;
}


MATHISFUNDLL_API int GetFibonacciNTerm(int Term)
{
  //https://www.mathsisfun.com/numbers/fibonacci-sequence.html
  //Recursive way of calculating fibonacci term. Not the best algorithm in terms of efficiency but it works in our case :)
  if (Term == 0)
  {
    return 0;
  }
  else if (Term == 1)
  {
    return 1;
  }
  else return GetFibonacciNTerm(Term - 1) + GetFibonacciNTerm(Term - 2);
}

此时,将解决方案配置设置为Release,将平台设置为x64,编译后即可关闭Visual Studio。完成后,就可以使用引擎生成的 dll 了。


4

使用dll文件


这个过程主要需要以下三个步骤

  • 创建一个指向我们要使用的 dll 的 dll 句柄
  • 创建一个 dll 导出,指向我们要使用的 dll 的函数
  • 调用指向函数

转到我之前创建的相同蓝图函数库,并添加以下代码:

private:


  /**
   * Reference of the dll handle
   */
  static void* DllHandle;
  
  /**
   * Attempts to point the dll handle to the dll location
   */
  static bool LoadDllHandle();




public:


  UFUNCTION(BlueprintCallable)
  static void PrintSumFromLib(float a, float b);


  UFUNCTION(BlueprintCallable)
  static void PrintSumFromDll(float a, float b);


  /* Term >=0 */
  UFUNCTION(BlueprintCallable)
  static void PrintFibonacciTerm(int32 Term);

然后,在源文件上:

void* UOrfeasBlueprintFunctionLibrary::DllHandle=nullptr;


bool UOrfeasBlueprintFunctionLibrary::LoadDllHandle()
{
  FString DllFilePath = FPaths::ProjectDir() + "/Binaries/Win64/MATHISFUNDLL.dll";
  if (FPaths::FileExists(DllFilePath))
  {
    DllHandle = FPlatformProcess::GetDllHandle(*DllFilePath);
  }
  return DllHandle!=nullptr;
}


void UOrfeasBlueprintFunctionLibrary::PrintSumFromDll(float a, float b)
{
  if (DllHandle || LoadDllHandle()) //We have a valid dll handle
  {
    //We will try to store the Sum function that exists in our loaded dll file in the DllExport
    //void* DllExport = FPlatformProcess::GetDllExport(DllHandle,*FString("AddNumbers"));
    void* DllExport = FPlatformProcess::GetDllExport(DllHandle,*FString("Sum"));
    if (DllExport)
    {
      //Declare a type definition for a function that accepts 2 float params and has float return type in order to store the Sum function from the dll
      typedef float(*GetSum)(float a, float b);
      
      //Type cast the valid dll export to GetSum type
      GetSum SumFunc = (GetSum)(DllExport);


      //Call the function & print the result
      float Result = (float)SumFunc(a,b);
      GLog->Log(FString::SanitizeFloat(a)+" + "+ FString::SanitizeFloat(b)+"="+FString::SanitizeFloat(Result));
    }
  }
}


void UOrfeasBlueprintFunctionLibrary::PrintFibonacciTerm(int32 Term)
{
  if (DllHandle || LoadDllHandle())
  {
    //Same approach as PrintSumFromDll
    void* DllExport = FPlatformProcess::GetDllExport(DllHandle,*FString("GetFibonacciNTerm"));
    if (DllExport)
    {
      typedef int32 (*GetFibonacciTerm)(int32 Term);
      GetFibonacciTerm FibonacciFunc = (GetFibonacciTerm)(DllExport);


      int32 FibonacciTerm = (int32)FibonacciFunc(Term);
      GLog->Log("Fibonacci Term #"+FString::FromInt(Term) +":"+FString::FromInt(FibonacciTerm));
    }
  }
}

此时,一旦编译完成,我们就可以通过 C++ 或 Blueprint 代码从 dll 文件中调用函数 Sum 和 GetFibonacciNTerm:

当然,后面可以编写更复杂的函数和逻辑,但这篇文章展示了使用 lib 和 dll 文件的基本设置。



相关推荐

教学楼里那种嵌着小石子的水磨石地面,是怎么整出来的? | 有趣的制造

今天的选题是之前小可爱「花凉」在后台发消息问的~看过以后念念不忘,满脑子都是小时候在教学楼冰冷地面上摔的跤,记不起来是不是在这种地面上磕掉的门牙...昨天发了预告后,有小可爱纷纷表示「就是这种地板,像...

教学楼里那种嵌着小石子的水磨石地面,是怎么整出来的?

话说有多少小可爱不想学习时,没事数着水磨石地面的小石子玩,然后互相评比哪颗石子最好看。到头来书又没有背完,课也没好好上,就怪地板有迷幻效果,扰乱了好好学习的坚定意志。(小编觉得即使换成瓷砖,你们也可能...

性能调优实战:Spring Boot 多线程处理SQL IN语句大量值的优化方案

环境:SpringBoot3.4.0...

RMAN备份监控及优化总结(rman全备份)

今天主要介绍一下如何对RMAN备份监控及优化,这里就不讲rman备份的一些原理了,仅供参考。一、监控RMAN备份1、确定备份源与备份设备的最大速度从磁盘读的速度和磁带写的带度、备份的速度不可能超出这两...

记Oracle中快速获取表及其各个字段注释的方法

简述java开发中,用过JPA的道友应该知道,我们可以通过写java代码自动生成对应的数据表;但这有个问题是,列名的注释并没有帮我们一起添加到数据库去,尤其在一些开发测试生产三个环境隔离的,就很不友好...

Oracle 数据库日常巡检之检查数据库cpu、I/O、内存性能

记录数据库的cpu使用、IO、内存等使用情况,使用vmstat,iostat,sar,top等命令进行信息收集并检查这些信息,判断资源使用情况。1.CPU使用情况:...

Oracle案例:ORA-00600: internal error code, arguments: 「4187」

本案例客户来自某省电信,alert日志大量的ORA-00600[4187]报错,已经影响到业务正常运行。...

MySQL索引失效的10大陷阱:从隐式类型转换到索引选择性全面优化

索引是MySQL性能优化的核心武器,但错误的使用场景可能让索引完全失效,导致查询性能断崖式下降。本文通过实际案例,深入剖析索引失效的典型场景及其底层原理,并提供可落地的解决方案。一、索引失效的核心原...

oracle查询语句执行计划分析(oracle如何查看sql执行计划)

1命令行开启配置#显示查询结果setautotraceon#不显示查询结果setautotracetraceonly2执行查询语句...

面试官:说说Oracle数据库result cache的原理是什么?

概述前面已经用实验给大家介绍了ResultCache相关内容,今天主要讨论一下Oracle11gResultCache的深层原理。从参数看,Oracle提供了ClientResultCac...

Oracle817 export 时ORA-06553和ORA-00904处理

现象:数据库版本8.1.7...

Oracle案例:一次gc buffer busy acquire诊断

本案例来自某客户两节点rac的一次生产故障,现象是大面积的gcbufferbusyacquire导致业务瘫痪。...

说文解字:“雪”字本身在造字时就很浪漫!

这是雪山的“雪”字。可是你知道吗?“雪”这个字其实和“山”是没有任何关系的。这个字下半部分“彐”并不是一座翻倒的山,而是一只手的意思。(凡是带“彐”的汉字,其实都和手有关。)“雪”字的商代甲骨文形状,...

应用最广的两类数据库的区别、优势对比、查询优化方法及案例实践

 1、通用数据库分类  1.1关系型数据库  关系型数据库是多个二维数据表的集合,数据以二维数据表的形式进行存储,数据表之间可以通过应用程序或者数据的主、外键建立特定的关联关系,让数据之间存在特定的...

【SQL】SQL 语法差异大全(PgSQL/MySQL/Oracle/TiDB/OceanBase)

以下是针对不同数据库系统的SQL语法差异总结,按功能分类展示:一、基础查询1.分页查询...