You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MSVS中用Clang编译GoogleTest出现undefined symbol链接错误

问题

在Windows系统下用Clang编译GoogleTest时,持续出现undefined symbol错误。我的解决方案包含两个项目:

  1. 静态库项目(cArray)
  2. 依赖cArray.lib的应用项目(cArray_tests)

两个项目的配置如下:

  • VC++目录 >> 库目录:添加了C:...\GoogleTest\1.14.0\build\lib\Debug
  • VC++目录 >> 包含目录:添加了
    • C:...\GoogleTest\1.14.0\googletest\include
    • C:...\GoogleTest\1.14.0\googlemock\include

MSVS输出的错误信息:

undefined symbol : void const* __cdecl testing::internal::GetTestTypeId(void)
undefined symbol : class testing::TestInfo* __cdecl testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, struct testing::internal::CodeLocation, void const*, void(__cdecl*)(void), void(__cdecl*)(void), class testing::internal::TestFactoryBase*)
undefined symbol : bool __cdecl testing::internal::IsTrue(bool)
undefined symbol : public: __cdecl testing::internal::GTestLog::GTestLog(enum testing::internal::GTestLogSeverity, char const*, int)
undefined symbol : public: __cdecl testing::internal::GTestLog::~GTestLog(void)
undefined symbol : protected: __cdecl testing::Test::Test(void)
undefined symbol : public: __cdecl testing::Message::Message(void)
undefined symbol : public: __cdecl testing::internal::AssertHelper::AssertHelper(enum testing::TestPartResult::Type, char const*, int, char const*)
undefined symbol : public: void __cdecl testing::internal::AssertHelper::operator=(class testing::Message const&) const
undefined symbol : public: __cdecl testing::internal::AssertHelper::~AssertHelper(void)
undefined symbol : public: virtual __cdecl testing::Test::~Test(void)
undefined symbol : class testing::AssertionResult __cdecl testing::AssertionSuccess(void)
undefined symbol : class testing::AssertionResult __cdecl testing::internal::EqFailure(char const*, char const*, class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char>> const&, class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char>> const&, bool)
undefined symbol : protected: virtual void __cdecl testing::Test::SetUp(void)
undefined symbol : protected: virtual void __cdecl testing::Test::TearDown(void)
undefined symbol : void __cdecl testing::InitGoogleTest(int*, char**)
undefined symbol : public: static class testing::UnitTest * __cdecl testing::UnitTest::GetInstance(void)
undefined symbol : public: int __cdecl testing::UnitTest::Run(void)

相关代码文件:

cArray_tests.cpp

#include "cArray.hpp"
#include <gtest/gtest.h>
#include <gmock/gmock.h>

namespace framework {
namespace testing  {
namespace          {

    class cArrayTest : public ::testing::Test 
    {
    protected:
        cArrayTest() {}
        ~cArrayTest() override {}
        void SetUp() override {}
        void TearDown() override {}
    };

    TEST(cArray, DefaultConstructor)
    {
        cArray<int> a1 = { 1, 2, 3, 4, 5, 6, 7 };

        //EXPECT_THAT(a1, ::testing::ElementsAre(1, 2, 3, 4, 5, 6, 7));
        EXPECT_EQ(a1.size(), 7);
    }
};
};
};

main.cpp

#include <iostream>
#include <string>
#include "cArray.hpp"
#include "cArray_tests.cpp"


int main(int argc, char ** argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
分析与解决方案

这些undefined symbol错误本质是链接器找不到GoogleTest相关函数的实现,核心是未正确链接GTest库或配置不匹配,具体排查和解决办法如下:

  1. 显式添加GTest库依赖
    仅配置库目录不够,需在测试项目(cArray_tests)的链接器 >> 输入 >> 附加依赖项中添加对应库文件:

    • Debug模式:gtestd.lib、gmockd.lib(若用了GMock),如果自己写了main函数可不用gtest_maind.lib,但建议添加
    • Release模式:gtest.lib、gmock.lib
  2. 统一编译配置
    GoogleTest的编译配置必须和你的项目完全一致:

    • 确保GTest是用Clang编译的(不能混用MSVC编译的GTest库)
    • 保持Debug/Release模式、平台(x86/x64)完全匹配
    • 同步运行时库设置:比如GTest用/MDd,你的项目也要在C/C++ >> 代码生成 >> 运行时库中设为/MDd
  3. 修复命名空间冲突
    测试代码嵌套了自定义的framework::testing命名空间,会和GTest的testing命名空间冲突,导致符号解析异常。建议将测试代码移出自定义的testing命名空间,或确保所有GTest API都用::testing前缀调用。

  4. 调整代码包含方式
    在main.cpp中直接#include "cArray_tests.cpp"会导致代码重复编译,引发符号冲突。正确做法是把cArray_tests.cpp作为源文件添加到测试项目中,而非在main里include它。

内容的提问来源于stack exchange,提问作者SKNB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 22:04:55