BOOST_ERROR_CODE_HEADER_ONLY does not have the advertised effect

前端 未结 1 1107
野性不改
野性不改 2021-01-19 13:30

In my app I include boost/system/error_code.hpp (boost 1.58) but don\'t want to link to boost_system, but instead have a header-only solution. I read by definin

相关标签:
1条回答
  • 2021-01-19 13:54

    I got it to work after some experimenting. The key for success here is to include the header boost/system/error_code.hpp before any other code that includes that header as well, with the BOOST_ERROR_CODE_HEADER_ONLY preprocessor symbol defined directly before the include, in a cpp file. It is essential to include it in a cpp file and not, e.g. in a precompiled header (stdafx.h, *_prefix.h etc.) because it includes code, which only works in an object file. Defining BOOST_ERROR_CODE_HEADER_ONLY at project/target level might work too, but since you only need this exactly once in your entire project/target it makes more sense to defined that right before you include the boost header the first time.

    If you follow this rule you will also not be affected by a duplicate symbol problem that may occur if you include the same system_category() code in multiple cpp files.

    You may still get problems when you compile your code in release mode, as the compiler may automatically remove the included code (if not used in that cpp file). So it's better to disable optimization for it. However, since you don't want that for your regular code, it makes sense to create an own cpp file just for this include and disable optimization for that file entirely. This is how I ended up finally.

    0 讨论(0)
提交回复
热议问题