c++ - 在全局变量的初始化/取消初始化中使用隐式加载的 DLL 中的函数

标签 c++ windows

考虑一个使用导入库 logging.lib 的 Windows 程序,这样 logging.dll 将在运行时自动加载/卸载。

另外,考虑主程序的翻译单元之一定义类类型的全局变量,如下所示:

struct InitGuard
{
    InitGuard()
    {
        logging::initialize();
    }

    ~InitGuard()
    {
        logging::deinitialize();
    }
};

InitGuard g_initGuard;

除了更好的方法是在函数 main 中实例化 InitGuard 之外:

  • 是否保证 logging.dll 在调用 InitGuard 的 c'tor 时已经加载?
  • 更重要的是,是否保证 logging.dllInitGuard 的驱动程序返回之前不会被卸载?

最佳答案

Is it guaranteed that logging.dll will already be loaded when the c'tor of InitGuard is invoked?

是的。在任何 EXE 代码开始运行之前,所有对static 链接 DLL 的引用都由 OS Loader 解析。

Even more importantly, is it guaranteed that logging.dll will not yet be unloaded before the d'tor of InitGuard has returned?

是的。在 EXE 代码运行完毕之前,操作系统不会释放静态链接的 DLL。

关于c++ - 在全局变量的初始化/取消初始化中使用隐式加载的 DLL 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57782516/

相关文章:

c++ - SetErrorMode 和/MDd (/MTd) 编译标志

c++ - Eigen,如何处理按元素除以零

c++ - 独立 exe : Where on disk to save config files and logs?

c++ - 使用其他目录中的库构建项目。 Windows , MinGW

c++ - Windows 文件权限作为字符串

windows - cmake:指定编译器的问题

windows - 不用 tty 访问 COIN$

c++ - 当我用 libgcc 替换 libstdc++ 时,为什么我的 C++ 程序可以正确链接

c++ - 赋值运算符的返回类型

c++ - iOS:在方法内编辑对象