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

66 篇文章 6 订阅

简述

  • 本例以插件形式测试
  • 使用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;
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值