【问题标题】:Undefined symbols for architecture x86_64 when writing unit tests with gtest and clang 12使用 gtest 和 clang 12 编写单元测试时架构 x86_64 的未定义符号
【发布时间】:2021-03-04 23:34:25
【问题描述】:

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

我的测试文件如下所示:

#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,但这并没有解决我的问题。

【问题讨论】:

  • Undefined symbols 错误一样,要修复错误,您需要链接到相应的库或添加相应的源文件。在您的情况下,您需要找到定义符号_LoginDatabase的库或文件。
  • 这个问题与您的单元测试没有链接到日志库(即定义 Log 类的库)一致。理想情况下,您的构建系统会知道,如果您的测试依赖于 libcommon,它们需要链接到日志库。
  • @NicholasM 不幸的是,如果我将 llibcommon 添加到测试的 CMakeLists.txt 的 target_link_libraries 中,我会得到 ld: library not found for -llibcommon。我也试过common,它没有给出那个错误,但它也没有解决原来的问题。我对 CMake 不是很有信心,你能给我一个提示吗?
  • 您应该尝试使用库的确切 CMake 目标名称。例如,如果你的 cmake 文件有 add_library(my_common_lib, ...)add_executable(my_tests, ...),你应该使用 target_link_libraries(my_tests PRIVATE my_common_lib)。您可能需要在单独的问题中发布您的 CMake 详细信息。

标签: c++ cmake clang googletest azerothcore


【解决方案1】:

你可以通过添加来解决:

LoginDatabaseWorkerPool LoginDatabase;

致您的FormulaTest.cpp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多