如何从C ++(MSVC 11)中的静态库强制包含静态对象 [英] How to force include static objects from a static library in C++ (MSVC 11)

查看:119
本文介绍了如何从C ++(MSVC 11)中的静态库强制包含静态对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在C ++文件中初始化一个静态对象,该对象试图将一个类自动注册到其构造函数中的工厂中(就像任何标准的自动注册问题一样).问题是,这被编译成一个静态库,并且链接到可执行文件时,它已经被优化了.对此应该有一个非常简单的解决方案,但是令人惊讶的是,看起来它并不是那么简单.

I am trying to initialize a static object in a C++ file which is trying to auto register a class to a factory in its constructor (like any standard auto-registration problem). The problem is, this is compiled to a static library, and while linking to an executable, that is optimized away. There should have been a very simple solution to that, but surprisingly, looks like it's not that simple.

这是我的课程:

Factory.h 中(静态lib项目的一部分)

In Factory.h (part of static lib project)

class DummyClass : public BaseFactoryClass
{
    int mDummyInt;
public:
    DummyClass()
    {
        std::cout << "Pretending to register myself to the factory here\n";
    }
};

在某些cpp中,假设 Circle.cpp (静态lib项目的一部分)

In some cpp, let's say Circle.cpp (still part of the static lib project)

static DummyClass dum;

main.cpp (可执行文件的一部分)

main.cpp (part of the executable)

//some code accessing the BaseFactoryClass of the Registered derived classes. 

现在,由于静态对象未在可执行项目中直接"使用,因此将其从链接库中跳过.

Now, since the static object is not 'directly' used in the executable project, it's skipped from the linked library.

我想让它在MS VC11(Visual Studio 2012)(和GCC 4.8.*,但稍后中再说)中工作.看看其他问题,到目前为止,我尝试了各种尝试,这些似乎都不起作用:

I want to make it work in MS VC11 (Visual Studio 2012) (and GCC 4.8.*, but that's for later). Looking at other questions, I tried various things so far, which don't seem to work:

  1. 仅从Visual支持类似/WHOLEARCHIVE的链接器选项 Studio 2015(我们正在使用VS 2012)
  2. 指定/OPT:NOREF应该起作用(我尝试将其与/OPT:NOICF等其他标志进行多种组合),但不适用于任何人.
  3. 我在头文件中尝试了#pragma注释(链接器,"/include:symbolName"),但是这给出了有关无法识别符号的链接器错误. (而且这在GCC中不起作用,但可能-整个存档在那里工作).
  1. Looks like /WHOLEARCHIVE linker option is only supported from Visual Studio 2015 (We're using VS 2012)
  2. Specifying /OPT:NOREF should have worked (I tried several combination of this with other flags like /OPT:NOICF), but it doesn't work for anyone.
  3. I tried #pragma comment (linker, "/include:symbolName") in the header file, but that gives a linker error about symbol being unrecognized. (And also that wouldn't work in GCC, but probably --whole-archive works there).

Visual Studio链接器设置中有一个标志,该标志允许单独链接所有对象文件,而不是静态库,但是我不想走那条路.另外,最好只想在我要自动注册的每个类的源(.cpp)中写一些东西,所有样板代码和宏都放在诸如BaseFactory.h等的中央头文件中.做到这一点的方法(即使在C ++ 11中也可以保证将对符号进行初始化)?希望使任何新开发人员都尽可能容易地注册新类.

There is a flag in Visual Studio Linker Settings that allows linking all object files individually rather than the static lib, but I don't want to go that route. Also, preferably I'd just want to write something in the source (.cpp) of each individual class I want to automatically register, with all the boiler plate code and macros being in a central header like BaseFactory.h etc. Is there any way to do it (even in C++ 11 where there's a guarantee that a symbol will be initialized)? Want to make the registration of a new class as easy as possible for any new developer.

推荐答案

在MSVC中,您可以使用链接器编译指示来实现此目的:

In MSVC you have a linker pragma you can use for that purpose:

#pragma comment (linker, "/export:_dum")

通过这种方式,每当运行链接器时,它都会在您的可执行文件或DLL中强制链接_dum.

This way whenever the linker is run, it will force link _dum in your executable or DLL.

不过,更好的方法是考虑使用DLL而不是静态库.这样一来,您就完全避免了这个问题,因为在DLL的每次加载时,都会初始化静态变量.

A better way, though, would be to consider using a DLL instead of a static library. Then you avoid this problem altogether since on each load of the DLL that static variable will be initialized.

这篇关于如何从C ++(MSVC 11)中的静态库强制包含静态对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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