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

本文介绍如何在UE4中通过插件形式引入第三方库MyTestLib,包括.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;
}

 

UE4调用动态,需要进行以下几个步骤: 1. 编写动态的头文件和源文件,并进行编译生成动态文件(.dll或.so文件)。 2. 在UE4项目中创建新的C++类或打开现有的C++类,并添加对动态头文件的包含语句。 3. 在C++类中声明动态函数的函数声明(函数名、参数类型和返回类型),并在其中使用动态函数。 4. 将动态文件(.dll或.so文件)复制到UE4项目的插件或游戏模块的Binaries/Win64目录中。 5. 在UE4项目的构建设置中添加对动态文件的引用。在UE4项目的.build.cs文件中添加以下代码: ``` PublicAdditionalLibraries.Add("MyDynamicLibrary.lib"); ``` 其中"MyDynamicLibrary.lib"是您的动态文件的名称。 6. 在UE4项目中使用动态函数时,需要使用动态的导出函数名(也称为C++符号)来调用该函数。您可以使用Windows的dumpbin工具或Linux的nm工具来查看动态的导出函数名。 下面是一个简单的例子,演示如何在UE4项目中调用一个名为"my_function"的动态函数: 1. 编写动态的头文件和源文件,并进行编译生成动态文件: ``` // MyDynamicLibrary.h #ifdef MYDYNAMICLIBRARY_EXPORTS #define MYDYNAMICLIBRARY_API __declspec(dllexport) #else #define MYDYNAMICLIBRARY_API __declspec(dllimport) #endif MYDYNAMICLIBRARY_API int my_function(int a, int b); // MyDynamicLibrary.cpp #include "MyDynamicLibrary.h" MYDYNAMICLIBRARY_API int my_function(int a, int b) { return a + b; } ``` 2. 在UE4项目中创建新的C++类或打开现有的C++类,并添加对动态头文件的包含语句: ``` #include "MyDynamicLibrary.h" ``` 3. 在C++类中声明动态函数的函数声明,并在其中使用动态函数: ``` // MyClass.h #pragma once #include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "MyClass.generated.h" UCLASS() class MYPROJECT_API AMyClass : public AActor { GENERATED_BODY() public: UFUNCTION(BlueprintCallable, Category="MyCategory") int MyFunction(int a, int b); }; // MyClass.cpp #include "MyClass.h" int MyFunction(int a, int b) { return my_function(a, b); } ``` 4. 将动态文件(.dll或.so文件)复制到UE4项目的插件或游戏模块的Binaries/Win64目录中。 5. 在UE4项目的构建设置中添加对动态文件的引用。在UE4项目的.build.cs文件中添加以下代码: ``` PublicAdditionalLibraries.Add("MyDynamicLibrary.lib"); ``` 6. 在UE4项目中使用动态函数时,需要使用动态的导出函数名(也称为C++符号)来调用该函数。您可以使用Windows的dumpbin工具或Linux的nm工具来查看动态的导出函数名。在此例中,导出函数名为"my_function"。 ``` // MyClass.cpp #include "MyClass.h" int MyFunction(int a, int b) { // 调用动态函数 int result = my_function(a, b); return result; } ``` 至此,您已经成功在UE4项目中调用动态函数。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值