Per Microsoft documentation (/EH (Exception handling model) | Microsoft Learn),
/EHa
overrides both/EHs
and/EHc
arguments
But, when these options are not provided by CMake to compiler directly - because of using Visual Studio generator, it seems that generated Visual Studio project property is set according to the last /EH?? compiler’s option specified. And, because default “/EHsc” is being placed at the end of the compiler’s option list, “/EHa” introduced via CXXFLAGS environment variable at the beginning of this list is ignored.
So, because behavior of compiler’s options for exception handling is defined by the Microsoft page mentioned, the expected behavior: when CMake does generate Visual Studio solution and project files with the command like
cmake -E env CXXFLAGS="/EHa" cmake -G "Visual Studio 17 2022" -A x64 -S . -B x64_v143
the expectation is that in the project file generated it would be present the setting
<ExceptionHandling>Async</ExceptionHandling>
But, the observed behavior is that
<ExceptionHandling>Sync</ExceptionHandling>
is generated (which corresponds to “/EHsc” option).
Maybe, in addition to the de-duplication of options it could be meaningful to implement options’ overrides as described in MSVC documentation, eliminating the logic “the last one option wins”?
To have it mentioned, the workaround is not to use environment variable CXXFLAGS but modify CMakeLists.txt file with something like
if (MSVC)
# Async exception handling
string(REPLACE "/EHsc" "/EHa" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
endif()
In general, it’s very similar to Compiler option stripped by Visual Studio generator? but for other MSVC option.