使用 gtest 和 clang 12 编写单元测试时,体系结构的未定义符号 x86_64

Undefined symbols for architecture x86_64 when writing unit tests with gtest and clang 12

我正在尝试为此文件中定义的某些函数添加单元测试:

我的测试文件如下所示:

#include "gtest/gtest.h"
#include "Formulas.h"
#include "SharedDefines.h"

using namespace acore::Honor;
using namespace acore::XP;

TEST(FormulasTest, hk_honor_at_level)
{
    EXPECT_EQ(hk_honor_at_level(80), 124);
    // some more checks here...
}

TEST(FormulasTest, GetGrayLevel)
{
    EXPECT_EQ(GetGrayLevel(0), 0);
    // some more checks here...
}

TEST(FormulasTest, GetColorCode)
{
    EXPECT_EQ(GetColorCode(60, 80), XP_RED);
    // some more checks here...
}

TEST(FormulasTest, GetZeroDifference)
{
    EXPECT_EQ(GetZeroDifference(1), 5);
    // some more checks here...
}

TEST(FormulasTest, BaseGain)
{
    // PROBLEM HERE
    EXPECT_EQ(BaseGain(60, 1, CONTENT_1_60), 0);
}

当我调用 Formulas.h 中定义的函数 BaseGain 时,问题就出现了:

Undefined symbols for architecture x86_64:
  "_LoginDatabase", referenced from:
      Log::outDB(LogTypes, char const*) in libcommon.a(Log.cpp.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [src/test/unit_tests] Error 1
make[2]: *** [src/test/CMakeFiles/unit_tests.dir/all] Error 2
make[1]: *** [src/test/CMakeFiles/unit_tests.dir/rule] Error 2
make: *** [unit_tests] Error 2

特别是,如果我删除对 sLog->outError:

的调用,问题就消失了
 sLog->outError("BaseGain: Unsupported content level %u", content);

来自 BaseGain.

但是,这个问题只发生在单元测试中,项目源代码的其余部分编译得很好。

要编译包含单元测试的项目,请将 -DBUILD_TESTING=1 参数传递给 cmake 命令。

编辑:我已经尝试将 common 添加到 src/test/CMakeLists.txttarget_link_libraries 但这并没有解决问题我.

你可以通过添加解决:

LoginDatabaseWorkerPool LoginDatabase;

给你的 FormulaTest.cpp.