【UE4 C++】調用外部鏈接庫: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文件部署

image

插件 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;
}

原文最早發布於:https://blog.csdn.net/Szu_IT_Man/article/details/58232638


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM