Does having binaries with mismatched optimization levels (-O3, -O2, -O1, -O0) cause stability issues?

Viewed 168

Does having binaries with mismatched optimization levels (-O3, -O2, -O1, -O0) cause stability issues? For example, if app.exe is -O2, and libapp.so is -O3, or app.o with -O2 and libxyz.a with -O3?

2 Answers

In general no, having mismatched optimisation levels does not cause stability issues.

In theory, a particularly badly written library could change its binary interface based on an implementation defined pre-processor macro that is defined based on optimisation level in which case there could be a problem.

This is not very common in my experience. A more common problem is the NDEBUG macro (and similar).

Optimization level alone cannot affect it but frequently optimization level is coupled with some MACROS that affect build resulting in ABI issues. For instance, in MSVC a dll built in release mode will likely cause crash if it is linked to debug mode application if it accepts a reference to std::string as input for some function (not that one should have a std::string or reference to it as input in a shared library).

Also if the library and application are built with different compilers/options there might be some ABI issues if library's interface uses any complex classes. You should check compilers documentation for that info.

Also, if there is a bug in the code then optimization level might change the behaviour of the bug leading to some odd inconsistencies between optimization levels.

Related