简体   繁体   English

防止cin,cout,cerr被实例化

[英]Preventing cin, cout, cerr from being instantiated

As stated in this answer, when including iostream library the binary size gets much larger because the std::cin , std::cout , std::cerr (and maybe more) objects are created thus occupying a lot of memory. 回答所述,当包含iostream库时,二进制大小变得更大,因为std::cinstd::coutstd::cerr (可能更多)对象被创建,因此占用了大量内存。

I often compile code for embedded platforms which have strict constraints on memory. 我经常为嵌入式平台编译代码,这些代码对内存有严格的限制。 After including the iostream library the binary gets so big that it won't fit into the internal flash memory. 在包含iostream库之后,二进制文件变得非常大,以至于它不适合内部闪存。 That in turn affects the libraries which can be included, because a lot of them include <iostream> . 这反过来会影响可以包含的库,因为其中很多都包含<iostream> One of example can be Protobuf library which I can't use across multiple projects as it includes iostream . 其中一个例子可能是Protobuf库,我不能在多个项目中使用它,因为它包含iostream

For those targets, which are run on an embedded platform, I don't need the objects to be created because I won't never use it. 对于那些在嵌入式平台上运行的目标,我不需要创建对象,因为我永远不会使用它。 The question is: can I somehow prevent from creation of those objects? 问题是:我可以以某种方式阻止这些对象的创建吗? Is there maybe some other workaround which allows to include libraries which use iostream and to not increase the binary size so much? 是否有其他一些解决方法允许包含使用iostream库并且不会如此增加二进制大小?

You cannot in standard ways, because <iostream> is required to define these variables. 您不能以标准方式,因为<iostream>需要定义这些变量。 If you do not want the variables then you have to include directly the headers (like <ios> , <streambuf> , <istream> , <ostream> and/or <iosfwd> ). 如果您不想要变量,则必须直接包含标题(如<ios><streambuf><istream><ostream>和/或<iosfwd> )。 I assume you want to include <iostream> for classes defined in those headers. 我假设您要为这些标头中定义的类包含<iostream> If some third party library that you use includes things that you do not want then you have to consult with authors of such library, there are no generic answers. 如果您使用的某些第三方库包含您不想要的内容,那么您必须咨询此类库的作者,则没有通用答案。

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

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