当我在现有的cpp中使用我的一个类实现添加此代码时
#include <iostream>
struct TestStruct{
TestStruct(int i)
{
std::cerr << i << std::endl;
x = i;
}
int x;
};
TestStruct t(8);
Run Code Online (Sandbox Code Playgroud)
它在main执行前打印8 .
但是当我创建新的空文件test.cpp并在其中放入相同的代码时,没有打印任何内容.我检查了这个cpp是否被编译和链接.所有cpp文件编译为静态lib,然后将此lib与main.o链接在可执行文件中.我只使用g ++ 5.3选项-std=C++14.
为什么在第二种情况下错过全局变量初始化?