How to put assert into release builds in C/C++

前端 未结 7 1079
野性不改
野性不改 2020-12-24 02:18

I need to only run ship build and I need to assert on certain condition in release build to see if the problem is fixed. How do I do it?

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-24 03:16

    The default behaviour of ASSERT is to abort the program under the Debug configuration, but this generally becomes a no-op under a Release configuration. I believe it does this by checking for the existence of the preprocessor NDEBUG macro. I'm not at work at the moment so cannot check this.

    I think the easiest way around this is to modify the Debug configuration to turn all optimisations up to the same level as Release (O2 from memory), and then re-build your software. This will give you the equivalent performance and speed of a Release build, but it will still define the NDEBUG preprocessor macro which means all failed ASSERTs will still cause the program to abort. Just remember to change the optimisation level back later, otherwise you will have trouble debugging under the Debug configuration.

    In general though, ASSERTs should only be used for programming preconditions and never to handle failures in shipping software. You want to fail quickly during development, but gracefully in front of a user.

提交回复
热议问题