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

标签 c++ shared-libraries

捕获加载的共享库中抛出的异常是否可移植? 我注意到它适用于 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/

相关文章:

c++ - 为什么 cmake_link_libraries 包含静态库?

c++ - 严重错误:找不到 'boost/uuid/uuid.hpp'文件

c++ - 帮助理解 C++、模板、operator() 的类示例代码

android - 将 .so 文件和 jar 包装器包含到 Android 项目中

linux - 有什么办法可以直接修改二进制文件的SONAME吗?

android - 无法使用 Android-NDK 编译外部预编译共享库

c++ - 如何在 C++ 中减少较小字符串中的较大字符串?可能是通过哈希?

c++ - 使用指令与使用声明

c++ - 使用函数指针作为回调

java - 列出 Galaxy S III 上所有可用的共享库