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

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

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

下面会展示如何创建一个库或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 文件的基本设置。



相关推荐

PromptDA:4K分辨率精准深度估计!(分辨率4k是多少p)

这里是FoxFeed,一个专注于科技的内容平台。背景介绍在计算机视觉领域,深度估计一直是一个重要的研究方向。近日,由DepthAnything团队开发的...

m4a怎么转换成mp3?教你这样转换音频格式

m4a怎么转换成mp3?M4A是MPEG-4音频标准的文件的扩展名,它可以存储各种类型的音频内容,运用比较广泛,尽管m4a被很多媒体应用兼容,但仍有很多应用无法打开它,将m4a转换成mp3就是一个很不...

“讲述初心故事 传递使命情怀”2019第五届江苏医院微电影节启动

“讲述初心故事传递使命情怀”,2019第五届江苏医院微电影节9月16日启动。江苏医院微电影节由新华网江苏有限公司和江苏省医院协会联合举办,扬子江药业集团协办,秉承“讲述初心故事传递使命情怀”为活动...

短视频宝贝=慢?阿里巴巴工程师这样秒开短视频

前言随着短视频兴起,各大APP中短视频随处可见,feeds流、详情页等等。怎样让用户有一个好的视频观看体验显得越来越重要了。大部分feeds里面滑动观看视频的时候,有明显的等待感,体验不是很好。针对这...

阿里巴巴工程师这样秒开短视频(阿里巴巴的工程师多少钱一个月)

前言随着短视频兴起,各大APP中短视频随处可见,feeds流、详情页等等。怎样让用户有一个好的视频观看体验显得越来越重要了。大部分feeds里面滑动观看视频的时候,有明显的等待感,体验不是很好。针对这...

旗鱼浏览器1.0 RC正式版候选版:增账户同步等

从9月19日发布第一个Beta版至今,约80天的时间便这么飞走了,作为2015年底的一个答卷,今天旗鱼浏览器1.0RC(正式版候选版)发布,如果没有意外,明天我们将发布电脑版和安卓版的第一个1.0正...

5种方法,教你将m3u8转换为mp4格式

m3u8格式在许播放器中不受支持,只能在浏览器中进行在线观看,然而,在线观看可能会不大方便,如果网络卡顿的话就会影响观感。想要将...

kgma格式怎么转换为mp3?试试这5种简单的音频转换方法!

由于kgma格式的特殊性和平台限制,除了专属的音乐平台外,其他设备和网络平台是无法识别或播放kgma格式的音乐的,因此为了方便使用,我们就必须将kgma格式转换为mp3。接下来,小编就为大家推荐5种简...

500+本程序员值得看的书籍,7大类,1大合集,收藏,日后有用

一、Golang书籍推荐入门《Go入门指南》...

教你编写最简单的CM3操作系统,160行实现任务创建与切换

如题,任务创建与上下文切换是跟硬件息息相关的,而这恰恰是RTOS编写的最难点,抛开这些功能,剩下的就是双向链表增删改操作了,本例用最精简的方式实现了任务创建与切换,OS启动等功能,并运用了Cortex...

Hot 3D 人体姿态估计 HPE Demo复现过程

视频讲解...

各编程语言相互调用示例,代码简单,生成的软件体积也很小

aardio支持混入很多不同的编程语言,代码简单,生成的软件体积也很小。下面看示例。...

你知道shell脚本中$0 $1 $# $@ $* $? $$ 都是什么意思吗?

一、概述shell中有两类字符:普通字符、元字符。1.普通字符...

NDK打印调用堆栈(logger.error打印堆栈信息)

虽然android源码里有android::CallStack用来打印堆栈,但是NDK里面并没有包含它,所以不能直接调用它,所以要尝试用动态调用的方式来实现。我测试的手机是安卓8.1.0版本,...

小白都能看得懂的Cgo入门教程(cgo2.0教程)

在Go语言开发过程中,尽管Go本身功能强大,但仍然有许多C语言库可以复用,如操作系统API、高性能计算库、数据库驱动等。Go提供了一种强大的机制——Cgo,让我们可以在Go代码中调用C...