bazel test 编译失败:googletest、gtest 报错

问题描述

bazel test 遇到很多奇怪的编译错误,报错位置位于“googletest”目录,而且没有修改过 googletest 源码:

ERROR: /bazel_cache/output_user_root/.../external/google/BUILD.bazel:80:11: Compiling googletest/src/gtest-matchers.cc failed: (Exit 1): gcc failed: error executing command (from target @google//:gtest) /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer '-std=c++0x' -MD -MF ... (remaining 34 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
In file included from external/google/googletest/include/gtest/gtest-printers.h:114,
                 from external/google/googletest/include/gtest/gtest-matchers.h:48,
                 from external/google/googletest/src/gtest-matchers.cc:35:
external/google/googletest/include/gtest/internal/gtest-internal.h:635:58: error: wrong number of template arguments (0, should be 1)
  635 |   typedef ::std::map<std::string, CodeLocation, std::less<>> RegisteredTestsMap;
      |                                                          ^
In file included from /usr/include/c++/9/memory:76,
                 from external/google/googletest/include/gtest/gtest-matchers.h:43,
                 from external/google/googletest/src/gtest-matchers.cc:35:
/usr/include/c++/9/bits/stl_function.h:381:12: note: provided for 'template<class _Tp> struct std::less'
  381 |     struct less : public binary_function<_Tp, _Tp, bool>
      |            ^~~~
In file included from external/google/googletest/include/gtest/gtest-printers.h:114,
                 from external/google/googletest/include/gtest/gtest-matchers.h:48,
                 from external/google/googletest/src/gtest-matchers.cc:35:
external/google/googletest/include/gtest/internal/gtest-internal.h:635:59: error: template argument 3 is invalid
  635 |   typedef ::std::map<std::string, CodeLocation, std::less<>> RegisteredTestsMap;
      |                                                           ^~
external/google/googletest/include/gtest/internal/gtest-internal.h: In member function 'bool testing::internal::TypedTestSuitePState::AddTestName(const char*, int, const char*, const char*)':
external/google/googletest/include/gtest/internal/gtest-internal.h:612:23: error: request for member 'insert' in '((testing::internal::TypedTestSuitePState*)this)->testing::internal::TypedTestSuitePState::registered_tests_', which is of non-class type 'testing::internal::TypedTestSuitePState::RegisteredTestsMap' {aka 'int'}
  612 |     registered_tests_.insert(
      |                       ^~~~~~
external/google/googletest/include/gtest/internal/gtest-internal.h: In member function 'bool testing::internal::TypedTestSuitePState::TestExists(const string&) const':
external/google/googletest/include/gtest/internal/gtest-internal.h:618:30: error: request for member 'count' in '((const testing::internal::TypedTestSuitePState*)this)->testing::internal::TypedTestSuitePState::registered_tests_', which is of non-class type 'const RegisteredTestsMap' {aka 'const int'}
  618 |     return registered_tests_.count(test_name) > 0;
      |                              ^~~~~
external/google/googletest/include/gtest/internal/gtest-internal.h: In member function 'const testing::internal::CodeLocation& testing::internal::TypedTestSuitePState::GetCodeLocation(const string&) const':
external/google/googletest/include/gtest/internal/gtest-internal.h:622:40: error: qualified-id in declaration before 'it'
  622 |     RegisteredTestsMap::const_iterator it = registered_tests_.find(test_name);
      |                                        ^~
In file included from external/google/googletest/include/gtest/internal/gtest-internal.h:42,
                 from external/google/googletest/include/gtest/gtest-printers.h:114,
                 from external/google/googletest/include/gtest/gtest-matchers.h:48,
                 from external/google/googletest/src/gtest-matchers.cc:35:
external/google/googletest/include/gtest/internal/gtest-internal.h:623:5: error: 'it' was not declared in this scope; did you mean 'int'?
  623 |     GTEST_CHECK_(it != registered_tests_.end());
      |     ^~~~~~~~~~~~
external/google/googletest/include/gtest/internal/gtest-internal.h:623:5: error: request for member 'end' in '((const testing::internal::TypedTestSuitePState*)this)->testing::internal::TypedTestSuitePState::registered_tests_', which is of non-class type 'const RegisteredTestsMap' {aka 'const int'}
  623 |     GTEST_CHECK_(it != registered_tests_.end());
      |     ^~~~~~~~~~~~
In file included from external/google/googletest/include/gtest/gtest-printers.h:114,
                 from external/google/googletest/include/gtest/gtest-matchers.h:48,
                 from external/google/googletest/src/gtest-matchers.cc:35:
external/google/googletest/include/gtest/internal/gtest-internal.h:624:12: error: 'it' was not declared in this scope; did you mean 'int'?
  624 |     return it->second;
      |            ^~
      |            int

问题分析

出现这个问题多半是因为没有启用 C++14 支持。即使你在 BUILD 文件中 cc_test 的 copts 里指定过 C++14,依然会出现该问题!

解决方案

bazel test 命令中增加 --cxxopt=-std=c++14 选项

bazel test --cxxopt=-std=c++14 //my_components/tests:my_test
posted @ 2023-08-06 17:34  Zijian/TENG  阅读(369)  评论(0编辑  收藏  举报