gpt4 book ai didi

c++ - 在加载的共享库中捕获异常

转载 作者:太空宇宙 更新时间:2023-11-04 14:23:48 24 4
gpt4 key购买 nike

捕获加载的共享库中抛出的异常是否可移植?我注意到它适用于 dlfcn.h,但我想知道这种行为是否通常是预期的,例如当在 Windows 上使用 LoadLibrary 时?

示例代码:

main.cpp:

#include <stdexcept>
#include <cstdio>
#include <dlfcn.h>

typedef void(*dummy_t)();

int main()
{
dummy_t f;
void* handle;
handle = dlopen("module.so", RTLD_LAZY);
f = (dummy_t)dlsym(handle, "modulemain");
try
{
f();
}
catch(std::runtime_error& e)
{
fprintf(stderr, "caught exception: %s\n", e.what());
}
dlclose(handle);
}

module.cpp:

#include <stdexcept>

extern "C" void modulemain()
{
throw std::runtime_error("some terrible error occured");
}

最佳答案

是的,在 Windows 下应该可以正常工作。

关于c++ - 在加载的共享库中捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5622867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com