为了在 Visual Studio 中创建 lib 文件,请搜索表达式“Static Libray”并创建一个新项目
在创建的示例项目中,添加了一个新的头文件和源文件,分别名为 OrfeasMathLibrary.h 和 OrfeasMathLibrary.cpp。下面是具体的代码:
//OrfeasMathLibrary.hnamespace 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 pathPublicAdditionalLibraries.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: