Windows GTest EXPECT_STREQ: error: no matching function for call to 'CmpHelperSTREQ'

Windows GTest EXPECT_STREQ: error: no matching function for call to 'CmpHelperSTREQ'

出于某种原因,GTest 在我的开发站上表现不佳。某些 ASSERT/EXPECT 测试正在运行,但我无法让字符串比较正常运行。这就是代码在 CLion 中的样子;注意错误弹出窗口:

下面还附上了编译时的错误输出。由于我在 Windows 10 上使用 JetBrains CLion,因此必须使用 "MinGW Makefiles" CMake 生成器构建 GTest,然后使用 MinGW make(而不是 CMake 默认的 Visual Studio 生成器)。此外,我能找到的唯一可用资源是最新的 Github GTest master 分支;其 2016 年 11 月的最新版本将不会基于 MinGW 中的 Windows。

In file included from C:/PROGRA~2/GOOGLE~1/include/gtest/gtest.h:1874:0,
                 from C:\projects\gtest-test\tests\basic_test.cpp:4:
C:\projects\gtest-test\tests\basic_test.cpp: In member function 'virtual void basic_test_helloWorldEqualsHelloWorld_Test::TestBody()':
C:/PROGRA~2/GOOGLE~1/include/gtest/gtest_pred_impl.h:147:45: error: no matching function for call to 'CmpHelperSTREQ(const char [7], const char [7], std::__cxx11::string&, std::__cxx11::string&)'
   GTEST_ASSERT_(pred_format(#v1, #v2, v1, v2), \
                                             ^
C:/PROGRA~2/GOOGLE~1/include/gtest/gtest_pred_impl.h:77:52: note: in definition of macro 'GTEST_ASSERT_'
   if (const ::testing::AssertionResult gtest_ar = (expression)) \
                                                    ^
C:/PROGRA~2/GOOGLE~1/include/gtest/gtest_pred_impl.h:162:3: note: in expansion of macro 'GTEST_PRED_FORMAT2_'
   GTEST_PRED_FORMAT2_(pred_format, v1, v2, GTEST_NONFATAL_FAILURE_)
   ^
C:/PROGRA~2/GOOGLE~1/include/gtest/gtest.h:1996:3: note: in expansion of macro 'EXPECT_PRED_FORMAT2'
   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, s1, s2)
   ^
C:\projects\gtest-test\tests\basic_test.cpp:14:5: note: in expansion of macro 'EXPECT_STREQ'
     EXPECT_STREQ(hello2, hello3);
     ^
In file included from C:\projects\gtest-test\tests\basic_test.cpp:4:0:
C:/PROGRA~2/GOOGLE~1/include/gtest/gtest.h:1537:28: note: candidate: testing::AssertionResult testing::internal::CmpHelperSTREQ(const char*, const char*, const char*, const char*)
 GTEST_API_ AssertionResult CmpHelperSTREQ(const char* s1_expression,
                            ^
C:/PROGRA~2/GOOGLE~1/include/gtest/gtest.h:1537:28: note:   no known conversion for argument 3 from 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'const char*'
C:/PROGRA~2/GOOGLE~1/include/gtest/gtest.h:1570:28: note: candidate: testing::AssertionResult testing::internal::CmpHelperSTREQ(const char*, const char*, const wchar_t*, const wchar_t*)
 GTEST_API_ AssertionResult CmpHelperSTREQ(const char* s1_expression,
                            ^
C:/PROGRA~2/GOOGLE~1/include/gtest/gtest.h:1570:28: note:   no known conversion for argument 3 from 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'const wchar_t*'
tests\CMakeFiles\gtestTest_tests.dir\build.make:62: recipe for target 'tests/CMakeFiles/gtestTest_tests.dir/basic_test.cpp.obj' failed
mingw32-make.exe[3]: *** [tests/CMakeFiles/gtestTest_tests.dir/basic_test.cpp.obj] Error 1
mingw32-make.exe[2]: *** [tests/CMakeFiles/gtestTest_tests.dir/all] Error 2
CMakeFiles\Makefile2:1063: recipe for target 'tests/CMakeFiles/gtestTest_tests.dir/all' failed
mingw32-make.exe[1]: *** [tests/CMakeFiles/gtestTest_tests.dir/rule] Error 2
CMakeFiles\Makefile2:1075: recipe for target 'tests/CMakeFiles/gtestTest_tests.dir/rule' failed
Makefile:494: recipe for target 'gtestTest_tests' failed
mingw32-make.exe: *** [gtestTest_tests] Error 2

您尝试使用 EXPECT_STREQ 来比较两个 std::string,而在比较原始 C 字符串 (char*) 时应该使用它。

Google Test Primer 中有一节是关于这个的。

为了比较 std::string,您应该使用 EXPECT_EQ

请为 Google 的测试使用嵌套源代码树 https://github.com/google/googletest.git 在你的项目中。

例如,如果您的项目文件夹是C:\projects\gtest-test\tests, 通过 shell 调用 将 GT 克隆到子文件夹 googletest (C:\projects\gtest-test\tests\googletest) git 克隆 https://github.com/google/googletest.git 来自 C:\projects\gtest-test\tests 目录。

你的 CMakeLists.txtC:\projects\gtest-test\tests 需要这样:

CMAKE_MINIMUM_REQUIRED(VERSION 3.3)
PROJECT(TestGoogleTargets)

add_definitions(-DGTEST_USE_OWN_TR1_TUPLE=1 -DGTEST_HAS_TR1_TUPLE=1)
IF (APPLE)
    add_definitions(-D__GLIBCXX__)
ENDIF (APPLE)

add_subdirectory(googletest)

INCLUDE_DIRECTORIES(${gtest_SOURCE_DIR}/include)
INCLUDE_DIRECTORIES(${gmock_SOURCE_DIR}/include)

ADD_EXECUTABLE(TestWithGMockMain dummyMock.cc)
TARGET_LINK_LIBRARIES(TestWithGMockMain gmock_main)

ADD_EXECUTABLE(TestWithGMock dummyMockWithMain.cc)
TARGET_LINK_LIBRARIES(TestWithGMock gmock)

ADD_EXECUTABLE(TestWithGTestMain dummy.cc)
TARGET_LINK_LIBRARIES(TestWithGTestMain gtest_main)

ADD_EXECUTABLE(TestWithGTest dummyWithMain.cc)
TARGET_LINK_LIBRARIES(TestWithGTest gtest)