lib文件中的c++全局类构造函数

c++ global class constructor in lib file

本文关键字:全局 构造函数 c++ 文件 lib      更新时间:2023-10-16

我有以下项目生成foo.lib.

//foo.h:
class A
{
public:
    A();
    static A* i;
};
//foo.cpp:
A* i = 0;
A g_A_instance; 
A::A()
{
    //this constructor is not called :(
    i = this;
}

我有另一个生成与foo.lib链接的exe文件的项目。似乎没有调用构造函数A::A()。我该怎么做呢?我缺少一些编译器选项吗?

PS:我使用了visualstudio编译器2008

谢谢,Raxvan。

如果我更改

A* i = 0;

A* A::i = 0;

所以它会链接,构造函数是在VS2005中为我正确调用的,默认选项。这个确切的例子在你的编译器上重现了这个问题吗?