static_lib_customizations.qbk 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. [/
  2. / Copyright (c) 2003 Boost.Test contributors
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section:static_lib_customizations Static-library variant customizations]
  8. [section:entry_point Customizing the module's entry point]
  9. In the static library variant, customizing the main entry point is quite troublesome, because the definition
  10. of function `main` is already compiled into the static library. This requires you to rebuild the __UTF__
  11. static library with the defined symbol __BOOST_TEST_NO_MAIN__. In the Boost root directory you need to
  12. invoke command
  13. ```
  14. > b2 --with-test link=static define=__BOOST_TEST_NO_MAIN__ define=__BOOST_TEST_ALTERNATIVE_INIT_API__ install
  15. ```
  16. [warning This removal of entry point definition from the static library will affect everybody else who is
  17. linking against the library. It may be less intrusive to switch to the
  18. [link boost_test.adv_scenarios.shared_lib_customizations shared library usage variant] instead.]
  19. In one of the source files, you now have to define your custom entry point, and invoke the default
  20. [link boost_test.adv_scenarios.test_module_runner_overview test runner] `unit_test_main` manually with
  21. the default [link boost_test.adv_scenarios.test_module_init_overview initialization function] `init_unit_test`
  22. as the first argument. There is no need to define __BOOST_TEST_NO_MAIN__ in your source code, but you need
  23. to define __BOOST_TEST_ALTERNATIVE_INIT_API__ in the main file:
  24. [table
  25. [[In *exactly one* file][In all other files]]
  26. [[```#define BOOST_TEST_MODULE test module name
  27. #define BOOST_TEST_ALTERNATIVE_INIT_API
  28. #include <boost/test/unit_test.hpp>
  29. // entry point:
  30. int main(int argc, char* argv[], char* envp[])
  31. {
  32. return utf::unit_test_main(init_unit_test, argc, argv);
  33. }
  34. ```]
  35. [```#include <boost/test/unit_test.hpp>
  36. //
  37. // test cases
  38. //
  39. //
  40. // test cases
  41. //
  42. ```]]
  43. ]
  44. [note The reason for defining __BOOST_TEST_ALTERNATIVE_INIT_API__ is described
  45. [link boost_test.adv_scenarios.obsolete_init_func here].]
  46. [endsect] [/section:entry_point]
  47. [section:init_func Customizing the module's initialization function]
  48. In the static library variant, customizing the main entry point is quite troublesome, because the default test
  49. runner compiled into the static library uses the obsolete initialization function signature. This requires you
  50. to rebuild the __UTF__ static library with the defined symbol __BOOST_TEST_ALTERNATIVE_INIT_API__. In the Boost
  51. root directory you need to invoke command
  52. ```
  53. > b2 --with-test link=static define=__BOOST_TEST_ALTERNATIVE_INIT_API__ install
  54. ```
  55. [warning This alteration of the static library will affect everybody else who is linking against the
  56. library. Consider using the [link boost_test.adv_scenarios.obsolete_init_func obsolete test initialization function],
  57. which requires no rebuilding. Alternatively, it may be less intrusive to switch to the
  58. [link boost_test.adv_scenarios.shared_lib_customizations shared library usage variant] instead.]
  59. In one of the source files, you now have to define your custom initialization function with signature:
  60. ```
  61. bool init_unit_test();
  62. ```
  63. The default [link boost_test.adv_scenarios.test_module_runner_overview test runner] will use it to initialize
  64. the test module. In your source code, you no longer define macro __BOOST_TEST_MODULE__; instead, you need to
  65. define __BOOST_TEST_ALTERNATIVE_INIT_API__ in the main file:
  66. [table
  67. [[In *exactly one* file][In all other files]]
  68. [[```#define BOOST_TEST_ALTERNATIVE_INIT_API
  69. #include <boost/test/unit_test.hpp>
  70. // init func:
  71. bool init_unit_test()
  72. {
  73. return true;
  74. }
  75. ```]
  76. [```#include <boost/test/unit_test.hpp>
  77. //
  78. // test cases
  79. //
  80. // test cases
  81. //
  82. ```]]
  83. ]
  84. For reporting errors that may occur during the initialization,
  85. * either you return `false` (valid only for the new API only, see __BOOST_TEST_ALTERNATIVE_INIT_API__)
  86. * or you raise an exception such as `std::runtime_error` or [classref boost::unit_test::framework::setup_error]
  87. An error reported in this function aborts the execution of the test module.
  88. [note The reason for defining __BOOST_TEST_ALTERNATIVE_INIT_API__ is described
  89. [link boost_test.adv_scenarios.obsolete_init_func here].]
  90. [endsect] [/section:init_func]
  91. [endsect] [/section:static_lib_customizations]