c++ - std::set_terminate() no longer works after linking to Jolt in CMake - Stack Overflow

If I do:#include <exception>#include <iostream>int main(int argc, char* argv[]){std::se

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
 |  Show 9 more comments

1 Answer 1

Reset to default 2

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745099560a4611191.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信