boost.test:在测试套件中包含STL代码时编译错误。

huangapple go评论102阅读模式
英文:

boost.test: compile errors when STL code is included within a test suite

问题

以下是翻译好的内容:

我的设置:

我的测试文件:

  1. // test.cpp
  2. #define BOOST_TEST_MAIN
  3. #define BOOST_TEST_MODULE a_nice_testing_attempt
  4. #include <boost/test/included/unit_test.hpp> // this is available
  5. BOOST_AUTO_TEST_SUITE(MyNiceTestSuite) // (*)
  6. #include <chrono>
  7. // actually I want to test much more, but I narrowed
  8. // the problem down to this minimal snippet
  9. BOOST_AUTO_TEST_SUITE_END() // (*)

然后我尝试编译如下:
g++ test.cpp -o test.exe

但出现了大量错误(精确到>1700行)。以下是开头部分的一些错误:

  1. In file included from C:/msys64/mingw64/include/c++/12.2.0/bits/chrono.h:37,
  2. from C:/msys64/mingw64/include/c++/12.2.0/chrono:39,
  3. from test_main.cpp:17:
  4. C:/msys64/mingw64/include/c++/12.2.0/ratio:58:24: error: expected template-name before '<' token
  5. 58 | : integral_constant<intmax_t, (_Pn < 0) ? -1 : 1>
  6. | ^
  7. C:/msys64/mingw64/include/c++/12.2.0/ratio:58:24: error: expected '{' before '<' token
  8. C:/msys64/mingw64/include/c++/12.2.0/ratio:63:24: error: expected template-name before '<' token
  9. 63 | : integral_constant<intmax_t, _Pn * __static_sign<_Pn>::value>
  10. | ^

以及底部的一些错误:

  1. C:/msys64/mingw64/include/c++/12.2.0/bits/chrono.h:1371:47: required from 'static MyNiceTestSuite:
  2. :std::chrono::time_point<MyNiceTestSuite::std::filesystem::__file_clock, _Dur> MyNiceTestSuite::std:
  3. :filesystem::__file_clock::_S_from_sys(const MyNiceTestSuite::std::chrono::time_point<MyNiceTestSuit
  4. e::std::chrono::_V2::system_clock, _Dur2>&) [with _Dur = MyNiceTestSuite::std::chrono::duration<long
  5. long int, MyNiceTestSuite::std::ratio<1, 1000000000> >]'
  6. C:/msys64/mingw64/include/c++/12.2.0/bits/chrono.h:1338:27: required from here
  7. C:/msys64/mingw64/include/c++/12.2.0/bits/chrono.h:1020:16: error: cannot convert '__time_point' {ak
  8. a 'MyNiceTestSuite::std::chrono::time_point<MyNiceTestSuite::std::filesystem::__file_clock, int>'} t
  9. o 'int' in return
  10. 1020 | return __time_point(__lhs.time_since_epoch() -__rhs);
  11. | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  12. | |
  13. | __time_point {aka MyNiceTestSuite::std::chrono::time_point<MyNiceTestSuite::s
  14. td::filesystem::__file_clock, int>}

如果删除标有星号的行(即与 BOOST_AUTO_TEST_SUITE 相关的行),则可以成功编译。不幸的是,我无法从这里进一步解决问题,我的问题相当模糊:

  • 这里发生了什么,我应该如何解决它?
英文:

My setup:

My test file:

  1. // test.cpp
  2. #define BOOST_TEST_MAIN
  3. #define BOOST_TEST_MODULE a_nice_testing_attempt
  4. #include <boost/test/included/unit_test.hpp> // this is available
  5. BOOST_AUTO_TEST_SUITE(MyNiceTestSuite) // (*)
  6. #include <chrono>
  7. // actually I want to test much more, but I narrowed
  8. // the problem down to this minimal snippet
  9. BOOST_AUTO_TEST_SUITE_END() // (*)

Then I try to compile as:
g++ test.cpp -o test.exe

And get a bunch of errors (>1700 lines to be precise). Here's some at the very top:

  1. In file included from C:/msys64/mingw64/include/c++/12.2.0/bits/chrono.h:37,
  2. from C:/msys64/mingw64/include/c++/12.2.0/chrono:39,
  3. from test_main.cpp:17:
  4. C:/msys64/mingw64/include/c++/12.2.0/ratio:58:24: error: expected template-name before '<' token
  5. 58 | : integral_constant<intmax_t, (_Pn < 0) ? -1 : 1>
  6. | ^
  7. C:/msys64/mingw64/include/c++/12.2.0/ratio:58:24: error: expected '{' before '<' token
  8. C:/msys64/mingw64/include/c++/12.2.0/ratio:63:24: error: expected template-name before '<' token
  9. 63 | : integral_constant<intmax_t, _Pn * __static_sign<_Pn>::value>
  10. | ^

And some from the bottom:

  1. C:/msys64/mingw64/include/c++/12.2.0/bits/chrono.h:1371:47: required from 'static MyNiceTestSuite:
  2. :std::chrono::time_point<MyNiceTestSuite::std::filesystem::__file_clock, _Dur> MyNiceTestSuite::std:
  3. :filesystem::__file_clock::_S_from_sys(const MyNiceTestSuite::std::chrono::time_point<MyNiceTestSuit
  4. e::std::chrono::_V2::system_clock, _Dur2>&) [with _Dur = MyNiceTestSuite::std::chrono::duration<long
  5. long int, MyNiceTestSuite::std::ratio<1, 1000000000> >]'
  6. C:/msys64/mingw64/include/c++/12.2.0/bits/chrono.h:1338:27: required from here
  7. C:/msys64/mingw64/include/c++/12.2.0/bits/chrono.h:1020:16: error: cannot convert '__time_point' {ak
  8. a 'MyNiceTestSuite::std::chrono::time_point<MyNiceTestSuite::std::filesystem::__file_clock, int>'} t
  9. o 'int' in return
  10. 1020 | return __time_point(__lhs.time_since_epoch() -__rhs);
  11. | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  12. | |
  13. | __time_point {aka MyNiceTestSuite::std::chrono::time_point<MyNiceTestSuite::s
  14. td::filesystem::__file_clock, int>}

If I remove the lines marked with the astherisk (i.e. BOOST_AUTO_TEST_SUITE related), then it compiles without a problem. Unfortunately I am unable to progress from here and my question is rather vague:

  • what is going on here and how could I solve it?

答案1

得分: 3

The BOOST_AUTO_TEST_SUITE宏引入了一个命名空间。标准库头文件只能包含在全局命名空间中,不能在其他地方包含。

这是一个示例,除了标准库之外不使用任何库,这应该导致类似于您的编译错误:

  1. #define ADD_NAMESPACE 1
  2. #if ADD_NAMESPACE
  3. namespace Foo
  4. {
  5. #endif
  6. #include <chrono>
  7. #if ADD_NAMESPACE
  8. }
  9. #endif

在线演示

(将 #define ADD_NAMESPACE 0 改为使编译正常工作。)

只需将包含部分移动即可正常工作。

  1. #include <chrono>
  2. #define BOOST_TEST_MAIN
  3. #define BOOST_TEST_MODULE a_nice_testing_attempt
  4. #include <boost/test/included/unit_test.hpp> // 这是可用的
  5. BOOST_AUTO_TEST_SUITE(MyNiceTestSuite) // (*)
  6. // 实际上我想测试更多东西,但我将问题缩小到这个最小的代码段
  7. BOOST_AUTO_TEST_SUITE_END() // (*)
英文:

The BOOST_AUTO_TEST_SUITE macro introduces a namespace. Standard library headers must not be included anywhere but in the global namespace.

This is a example not using any libs other than the standard library which should result in compiler errors similar to yours:

  1. #define ADD_NAMESPACE 1
  2. #if ADD_NAMESPACE
  3. namespace Foo
  4. {
  5. #endif
  6. #include &lt;chrono&gt;
  7. #if ADD_NAMESPACE
  8. }
  9. #endif

Godbolt Demo

(change to #define ADD_NAMESPACE 0 to make the compilation work.)

Simply moving the include should work.

  1. #include &lt;chrono&gt;
  2. #define BOOST_TEST_MAIN
  3. #define BOOST_TEST_MODULE a_nice_testing_attempt
  4. #include &lt;boost/test/included/unit_test.hpp&gt; // this is available
  5. BOOST_AUTO_TEST_SUITE(MyNiceTestSuite) // (*)
  6. // actually I want to test much more, but I narrowed
  7. // the problem down to this minimal snippet
  8. BOOST_AUTO_TEST_SUITE_END() // (*)

huangapple
  • 本文由 发表于 2023年6月8日 00:41:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76425466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定