Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::set_terminate() no longer works after linking to Jolt in CMake

Tags:

c++

cmake

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.com/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.

like image 461
Zebrafish Avatar asked Oct 16 '25 13:10

Zebrafish


1 Answers

To 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.

like image 110
Osyotr Avatar answered Oct 19 '25 05:10

Osyotr