C++ Segmentation Fault when using cout in static variable initialization

后端 未结 3 1729
攒了一身酷
攒了一身酷 2021-02-19 16:57

I have a program where I use cout to emit debug information. The code is executed in the initialization of a static global variable, i.e. quite early in the program execution. W

3条回答
  •  花落未央
    2021-02-19 17:55

    As Luchian has pointed out, you cannot use std::cout before the first instance of ios_base::Init has been constructed. You don't have to define an instance, however; including should be enough.

    Order of initialization is defined within a single translation unit. If you include at the top of all files which have static instances, you should be OK. If the constructor of a static object calls a function in another translation unit, however, and the output is in that translation unit, it is not sufficient to include only in the translation unit which does the output. You must include it in the translation unit where the static variable(s) are defined. Even if they don't do any output.

提交回复
热议问题