简体   繁体   English

如何在静态链接库中强制构造全局对象? [MSVC9]

[英]How does one force construction of a global object in a statically linked library? [MSVC9]

I have a global list of function pointers. 我有函数指针的全局列表。 This list should be populated at startup. 该列表应在启动时填充。 Order is not important and there are no dependencies that would complicate static initialization. 顺序并不重要,并且没有依赖关系会使静态初始化复杂化。 To facilitate this, I've written a class that adds a single entry to this list in its constructor, and scatter global instances of this class via a macro where necessary. 为方便起见,我编写了一个在其构造函数中向此列表添加单个条目的类,并在必要时通过宏分散了该类的全局实例。 One of the primary goals of this approach is to remove the need for explicitly referencing every instance of this class externally, instead allowing each file that needs to register something in the list to do it independently. 这种方法的主要目标之一是消除了从外部显式引用此类的每个实例的需要,而是允许需要在列表中注册某些内容的每个文件独立地进行操作。 Nice and clean. 干净整洁。

However, when placing these objects in a static library, the linker discards (or rather never links in) these units because no code in them is explicitly referenced. 但是,将这些对象放置在静态库中时,链接器会丢弃(或者永远不要链接)这些单元,因为其中没有明确引用它们中的代码。 Explicitly referencing symbols in the compilation units would be counterproductive, directly contradicting one of the main goals of the approach. 在编译单元中明确引用符号会适得其反,直接与该方法的主要目标之一背道而驰。 For the same reason, /INCLUDE is not an acceptable option, and /OPT:NOREF is not actually related to this problem. 出于相同的原因,/ INCLUDE不是可接受的选项,并且/ OPT:NOREF实际上与此问题无关。

Metrowerks has a __declspec directive for it, GCC has -force_load, but I cannot find any equivalent for MSVC. Metrowerks为它提供了__declspec指令,GCC具有-force_load,但是我找不到MSVC的任何等效项。

If you don't want to #include anything, then this won't work, but if you can live with that, a common technique is to use a nifty-counter to call a function declared in the cpp file before main, which would then initialize any static objects in said file. 如果您不想#include任何东西,那将行不通,但是如果您可以忍受,一种常见的技术是使用一个漂亮的计数器在main之前调用cpp文件中声明的函数,这将然后初始化所述文件中的所有静态对象。

I've used this in projects that had a lot of objects that should go into factory. 我已经在有很多对象应该进入工厂的项目中使用了它。 We had to have one file that #include each header that declared a class that must register itself in the factory. 我们必须有一个文件,该文件#include该声明必须在工厂登记本身就是一个类中的每个标题。 It's reasonably doable IMHO, as I strongly prefer writing source files as opposed to setting various linker/compiler options. 这是合理可行的恕我直言,因为我极喜欢编写源文件,而不是设置各种链接器/编译器选项。

Another way ( that doesn't seem to work via static libs, see comments ) could be to declare something as __declspec(dllexport) , which would force its inclusion (__declspec works in executables too). 另一种方式( 似乎无法通过静态库运行,请参见注释 )可以是将某些内容声明为__declspec(dllexport) ,这将强制将其包含(__declspec也可在可执行文件中使用)。

I think you want a #pragma optimize("", off) and #pragma optimize("", on) pair around these variable declarations. 我认为您希望在这些变量声明周围使用#pragma optimize("", off)#pragma optimize("", on)对。 That will turn off the optimization which is discarding them. 这将关闭正在丢弃它们的优化。

Also remember preprocessor directives can't be part of macros. 还请记住,预处理器指令不能成为宏的一部分。 __pragma can be used for some things (and is designed specifically to be used in macros), but not everything, and the syntax is a little different. __pragma可以用于某些事情(专门用于宏),但不能用于所有事物,并且语法略有不同。 Not sure if it works with optimize. 不知道它是否可以与优化一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM