Google Mock和Vcpkg无法解决的外部符号错误 [英] Unresolved external symbol error with Google Mock and Vcpkg
问题描述
我用一个模拟类创建了一个简单的C ++测试项目:
I have created a simple C++ test project with one mocking class:
#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调试项目.
I use Visual Studio 2017 (15.3.3). This is a x86 debug project with all default settings.
我已将gtest.lib和gmock.lib添加到链接器.编译和链接时,出现此错误:
I have added gtest.lib and gmock.lib to linker. When I compile and link, I get this error:
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项目进行了测试,但得到了相同的错误.
I have downloaded the gtest package with Vcpkg. It is compiled as a 32-bit DLL. Gtest is working fine, but when I instantiate the mock class I get the linking error. I have also tested with a CMake project and I get the same error.
推荐答案
来自MuratŞeker的评论:
Taken from Murat Şeker's comment:
将"GTEST_LINKED_AS_SHARED_LIBRARY"预处理程序定义添加到您的项目中.参见:github.com/google/googletest/issues/292
Add "GTEST_LINKED_AS_SHARED_LIBRARY" preprocessor definiton to your project. See : github.com/google/googletest/issues/292
这对我有用.
这篇关于Google Mock和Vcpkg无法解决的外部符号错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!