gpt4 book ai didi

c++ - Google Mock 和 Vcpkg Unresolved external symbol 错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:08:22 29 4
gpt4 key购买 nike

我用一个模拟类创建了一个简单的 C++ 测试项目:

#include <gtest/gtest.h>
#include <gmock/gmock.h>

class TestMock
{
public:
MOCK_CONST_METHOD0(Method1, void());
};

TEST(Test, Test1)
{
TestMock mock;
}

int main(int argc, char * argv[])
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

我使用 Visual Studio 2017 (15.3.3)。这是一个具有所有默认设置的 x86 调试项目。

我已将 gtest.lib 和 gmock.lib 添加到链接器。当我编译和链接时,我得到这个错误:

1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
1>ConsoleApplication1.obj : error LNK2001: unresolved external symbol "class testing::internal::Mutex testing::internal::g_linked_ptr_mutex" (?g_linked_ptr_mutex@internal@testing@@3VMutex@12@A)
1>ConsoleApplication1.obj : error LNK2001: unresolved external symbol "class testing::internal::Mutex testing::internal::g_gmock_mutex" (?g_gmock_mutex@internal@testing@@3VMutex@12@A)
1>d:\Develop\CPP\ConsoleApplication1\Debug\ConsoleApplication1.exe : fatal error LNK1120: 2 unresolved externals
1>Done building project "ConsoleApplication1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我已经用Vcpkg 下载了gtest 包。它被编译为 32 位 DLL。 Gtest 工作正常,但是当我实例化模拟类时出现链接错误。我还使用 CMake 项目进行了测试,但我遇到了同样的错误。

最佳答案

摘自 Murat Şeker 的评论:

Add "GTEST_LINKED_AS_SHARED_LIBRARY" preprocessor definiton to your project. See : github.com/google/googletest/issues/292

这对我有用。

关于c++ - Google Mock 和 Vcpkg Unresolved external symbol 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46025439/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com