__attribute__((init_priority(X))) 在 GCC [英] __attribute__((init_priority(X))) in GCC

查看:25
本文介绍了__attribute__((init_priority(X))) 在 GCC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 GCC 中使用 __attribute__((init_priority(X))),如下所示:

I'm using __attribute__((init_priority(X))) in GCC like this:

Type1 __attribute__ ((init_priority (101))) name1 = value1;
Type2 __attribute__ ((init_priority (102))) name2 = value2;

在不同的源文件中.假设 file1.cpp 和 file2.cpp.如果我在同一个库中使用它,它按预期工作,name1 在 name2 之前初始化,但如果我在不同的库中使用它,初始化顺序不是预期的.我在 gcc 文档上读到,这应该像我期望的那样在不同的库中工作,以定义初始化的顺序.我使用它的方式有什么问题吗?你有同样的问题吗?

in different source files. Let's say file1.cpp and file2.cpp. If I use this in same library it works as expected, name1 is initialized before name2 but if I use this in different libraries the order of initialization is not the expected one. I read on gcc documentation that this should work in different libraries as I expect, to define the order of initialization. Is there something wrong in the way I use this? Did you have same problem?

PS:重构不是解决这个问题的办法,因为我必须从 Visual Studio 移植一个非常大的项目.

PS: refactoring is not a solution for this problem because I must port a very big project from Visual Studio.

推荐答案

gcc 文档(gcc 4.4)说:

The gcc documentation (gcc 4.4) says:

`init_priority(优先级)'

`init_priority (PRIORITY)'

在标准 C++ 中,保证在命名空间范围内定义的对象严格按照顺序初始化它们的定义在给定的翻译单元中.没有保证用于跨翻译单元的初始化.然而,GNUC++允许用户控制对象的初始化顺序在命名空间范围内使用 `init_priority' 属性定义指定一个相对优先级,一个常数积分表达式目前介于 101 和 65535 之间.较低的数字表示更高的优先级.

In Standard C++, objects defined at namespace scope are guaranteed to be initialized in an order in strict accordance with that of their definitions in a given translation unit. No guarantee is made for initializations across translation units. However, GNU C++ allows users to control the order of initialization of objects defined at namespace scope with the `init_priority' attribute by specifying a relative PRIORITY, a constant integral expression currently bounded between 101 and 65535 inclusive. Lower numbers indicate a higher priority.

没有任何迹象表明这如何适用于库,尤其是共享库.我希望静态库(libxyz.a)在这方面与单个目标文件一样工作,因为它们只是目标文件的集合,并且文档的措辞表明它可以跨翻译单元(即使用不同的对象文件).

Nowhere is there any indication of how this applies with respect to libraries, especially shared libraries. I would expect static libraries (libxyz.a) to work the same as individual object files in this respect, since they are just a collection of object files, and the wording of the documentation suggests that it works across translation units (i.e. with different object files).

但是,共享库本身就是有效的可执行文件 --- 在给定的共享库中,初始化是按照指定的顺序完成的,但是共享库作为一个整体按照动态加载程序(即 liba)指定的顺序进行初始化.so 根据加载器的排序标准在 libb.so 之前或之后加载,并且 init_priority 属性不会影响该排序.

However, shared libraries are effectively executables in their own right --- within a given shared library the initialization is done in the specified order, but shared libraries are initialized as a whole in the order specified by the dynamic loader i.e. liba.so is loaded either before or after libb.so based on the ordering criteria of the loader, and the init_priority attribute cannot affect that ordering.

这篇关于__attribute__((init_priority(X))) 在 GCC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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