If I do:
#include <exception>
#include <iostream>
int main(int argc, char* argv[])
{
std::set_terminate([]() {
std::cout << "Called terminate handler";
});
std::terminate();
}
The terminate handler gets called. However if in my CMake project I do:
add_subdirectory(my_library_directory)
target_link_libraries(my_target PUBLIC Jolt)
Specifically the target_link_libraries
command then makes it so that my set_terminate()
function in my main doesn't work anymore. My handler isn't called anymore. This seems like really weird behaviour, and it's difficult to figure out what's happening because Jolt links to a bunch of other libraries and dependencies, and I can't comment things out to figure out what's causing it because if I comment something out the program will no longer compile or link.
Is there any reason why this may be the case? Obviously I know it's the specific command:
target_link_libraries(my_target PUBLIC Jolt)
That's causing this problem, is there like a setting or a compiler flag that could have been set that when I link to this library disables the terminate handler being called?
I provided a minimal reproducible example at:
A person on the Jolt Github said that it was due to:
CPP_EXCEPTIONS_ENABLED
was not set to true. It is off by default and it must do something to disable exceptions. I'm not sure exactly if it's the removal of the /EHsc in MSVC, or this:
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")
Any project linking to it must have these flags overridden.
If I do:
#include <exception>
#include <iostream>
int main(int argc, char* argv[])
{
std::set_terminate([]() {
std::cout << "Called terminate handler";
});
std::terminate();
}
The terminate handler gets called. However if in my CMake project I do:
add_subdirectory(my_library_directory)
target_link_libraries(my_target PUBLIC Jolt)
Specifically the target_link_libraries
command then makes it so that my set_terminate()
function in my main doesn't work anymore. My handler isn't called anymore. This seems like really weird behaviour, and it's difficult to figure out what's happening because Jolt links to a bunch of other libraries and dependencies, and I can't comment things out to figure out what's causing it because if I comment something out the program will no longer compile or link.
Is there any reason why this may be the case? Obviously I know it's the specific command:
target_link_libraries(my_target PUBLIC Jolt)
That's causing this problem, is there like a setting or a compiler flag that could have been set that when I link to this library disables the terminate handler being called?
I provided a minimal reproducible example at:
https://github/Please-just-dont/LinkingJoltExample
A person on the Jolt Github said that it was due to:
CPP_EXCEPTIONS_ENABLED
was not set to true. It is off by default and it must do something to disable exceptions. I'm not sure exactly if it's the removal of the /EHsc in MSVC, or this:
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")
Any project linking to it must have these flags overridden.
Share Improve this question edited Mar 3 at 21:55 Zebrafish asked Mar 3 at 10:05 ZebrafishZebrafish 15.1k3 gold badges66 silver badges154 bronze badges 14- are both calls consecutive ? if not then maybe that library also sets its own terminate handler – Ahmed AEK Commented Mar 3 at 10:12
- 5 ref: "f shall terminate execution of the program without returning to its caller, otherwise the behavior is undefined." – Mat Commented Mar 3 at 10:19
- @Mat I tried adding std::abort() and std::exit(0); in my terminate function and that didn't change anything. It's another issue I think. – Zebrafish Commented Mar 3 at 11:25
- How are you checking to see if your handler is called: debugger, log output...? – G.M. Commented Mar 3 at 11:32
- 2 Please provide minimal reproducible example. I can't reproduce. – Marek R Commented Mar 3 at 12:02
1 Answer
Reset to default 2To use std::terminate
you should have exceptions enabled. Otherwise a call to std::terminate
is a no-op:
_EXPORT_STD inline terminate_handler __CRTDECL set_terminate(terminate_handler) noexcept {
// register a terminate handler
return nullptr;
}
Why are exceptions disabled? Jolt library disabled them!
Either call set(CPP_EXCEPTIONS_ENABLED ON)
before add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/JoltPhysics-master/Build)
or pass it to CMake command line.