主程序和共享库在 __static_initialization_and_destruction_0 中初始化相同的静态变量 [英] Main Program and Shared Library initializes same static variable in __static_initialization_and_destruction_0

查看:63
本文介绍了主程序和共享库在 __static_initialization_and_destruction_0 中初始化相同的静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道为什么在 dlopen() 中初始化的库会初始化主程序拥有的静态变量.主程序和共享库都有静态变量的副本,但是由于某种原因共享库重新初始化了主程序的静态变量副本并销毁,导致主程序试图销毁时出现段错误.

Does anyone know why a library initialized within dlopen() would initialize a static variable owned by the main program. Both the main program and shared library have a copy of the static variable, but for some reason the shared library re-initializes the main program's copy of the static variable and destructs it, causing a segfault when the main program attempts to destruct it.

这是符号表中的错误名称修改的情况吗?

Is this a case of bad name mangling in the symbol table?

推荐答案

在这种情况下,运行时链接器只需要进程中符号的单个活动副本.如果共享对象和可执行文件都有该符号的副本,则运行时链接程序将解析对其中之一的所有引用.

This is a case where the runtime linker only wants a single active copy of a symbol in a process. If both a shared object and the executable have a copy of the symbol, the runtime linker will resolve all references to one of those.

您可以通过使用 version 命令.确保静态变量的符号不是全局的,您将获得您正在寻找的行为.

What you can do to solve this problem is to use symbol reduction using the version command of the link editor when building the shared object. Make sure the symbol for the static variable is not global and you will get the behavior you are looking for.

这篇关于主程序和共享库在 __static_initialization_and_destruction_0 中初始化相同的静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆