【问题标题】:How to setup {fmt} library for Unreal Engine project?如何为虚幻引擎项目设置 {fmt} 库?
【发布时间】:2022-11-25 01:54:18
【问题描述】:

我正在尝试为 UE4 项目设置 fmt,但仍然出现编译器错误。

使用的工具链:MSVC\14.16.27023

fmt lib 是从源代码构建的。

我用谷歌搜索了this issue 和未定义的检查宏。

#undef check
#include <fmt/format.h>

void test()
{
    auto test = fmt::format("Number is {}", 42);
}

获取此编译器错误:

我试过这个定义,但仍然无法编译。

#define FMT_USE_CONSTEXPR 0
#define FMT_HEADER_ONLY

也许有人在虚幻引擎项目中管理使用 fmt 库并可以分享一些经验?

【问题讨论】:

  • 您是否尝试过不使用二进制文件进行编译?如果您以仅标头模式使用该库,则无需构建它或包含二进制文件。您只需要在 #define FMT_HEADER_ONLY 之后包含所需的头文件,请参阅 here

标签: c++ unreal-engine4 fmt


【解决方案1】:

在虚幻引擎中集成 {fmt} 库有两个主要问题。

  1. 检查宏的全局定义。 fmt 实现中的一些变量/函数名为“check”。所以有冲突。
  2. 默认启用错误警告。所以你要么禁用它,要么抑制特定的警告。

    我最终为我的 UE 项目找到了这个解决方案。我定义了自己的标头包装器

    我的工程文件.h

    #pragma once
    
    #define FMT_HEADER_ONLY
    
    #pragma push_macro("check") // memorize current check macro
    #undef check // workaround to compile fmt library with UE's global check macros
    
    #ifdef _MSC_VER
        #pragma warning(push)
        #pragma warning(disable : 4583)
        #pragma warning(disable : 4582)
        #include "ThirdParty/fmt/Public/fmt/format.h"
        #include "ThirdParty/fmt/Public/fmt/xchar.h" // wchar support
        #pragma warning(pop)
    #else
        #include "ThirdParty/fmt/Public/fmt/format.h"
        #include "ThirdParty/fmt/Public/fmt/xchar.h" // wchar support
    #endif
    
    
    #pragma pop_macro("check") // restore check macro
    

    然后像这样在你的项目中使用它:

    SomeActor.cpp

    #include "MyProjectFmt.h"
    
    void SomeActor::BeginPlay()
    {
        std::string TestOne = fmt::format("Number is {}", 42);
        std::wstring TestTwo = fmt::format(L"Number is {}", 42);
    }
    

    此外,您可以围绕它创建一些宏包装器,以将所有角色放入虚幻引擎的 TEXT() 宏中,或者甚至编写自定义 back_inserter 以将字符直接格式化为 FString。这很容易实现,但这是另一回事了。

【讨论】:

    猜你喜欢
    • 2015-09-24
    • 2020-05-25
    • 2020-09-04
    • 1970-01-01
    • 2018-02-18
    • 2016-03-07
    • 2016-01-01
    • 2022-10-22
    • 2015-09-23
    相关资源
    最近更新 更多