0

There's a code that includes a lot of standard library headers. The code is situated in one file, and I cannot create any other C++ source files in that project (sic). I would like to increase the performance of the build process using precompiled headers. There're two problems

  1. Trying to use this guide on vector, I've got a file format not recognized error. What flags should I set to show gcc that it's a header file?
  2. There's a quote in official guide that says "Only one precompiled header can be used in a particular compilation". How do I precompile several headers at once then?

(Any batch/shell scripts appreciated too.)

Community
  • 1
  • 1
polkovnikov.ph
  • 6,256
  • 6
  • 44
  • 79
  • 3
    one precompiled header can include multiple headers. so you can include all stl headers in one header and use it – Bryan Chen Jun 19 '14 at 08:36

1 Answers1

0

Didn't you forget to use -x option for compiling your precompiled header as documentation states? Next if you want to have Standard Library's templates precompiled - it's impossible by definition. Using C++11 you can explicitly instantiate some templates in some particular translation unit for certain type arguments. From precompiled header you can specify

extern template void foo<char>(...)

What's the use of precompiled headers if you are trying to use them against templates? Your precompiled header must be a set of other includes/macros definitions/inline functions and everything else that can be safely included into multiple translation units. And yet - only one precompiled header per library/binary.

Zorgiev
  • 794
  • 1
  • 7
  • 19
  • 1
    AFAIK, precompiled headers are not translation units. There's no need in extern templates. – polkovnikov.ph Jun 19 '14 at 09:28
  • `-x` option is an answer to the first part of the question. It should go like `g++ -x c++-header -std=c++11 ...\vector` – polkovnikov.ph Jun 19 '14 at 09:28
  • It is in fact translation unit because ordinary includes are included textually as is instead of their corresponding "#include" directives. – Zorgiev Jun 19 '14 at 09:41
  • To precompile several headers at once - include all such headers of interest into single header that must be precompiled. – Zorgiev Jun 19 '14 at 09:42
  • And precompile this single header. – Zorgiev Jun 19 '14 at 10:06
  • BTW regarding `extern template` you're wrong too. http://stackoverflow.com/questions/8130602/using-extern-template-c0x – polkovnikov.ph Jun 19 '14 at 10:23
  • I have only one source file. It's impossible to create a header with all the other headers. It's explicitly said in the lines of the quesion. – polkovnikov.ph Jun 19 '14 at 10:24
  • If you are limited to the single c++ file no matter what sort of it is, i.e. whether it is a cpp or hpp file, then - better solution is just to use single cpp file with all your includes specified in the very beginning. This way you will avoid a situation when pch is generated and you need to link your object file with this pch. Anyways precompilation also processes your includes almost the same way like compiler it does after M4 processed #include directives. So either way you won't avoid a situation when you need to process these includes. – Zorgiev Jun 19 '14 at 10:39
  • Dude, it sounds kinda joke. It's an interview question. Isn't it? I don't get the point of doing so. In general I try to avoid of precompiled headers in a way of including only neccessary headers into translation units, but try to reduce includes in header files since these are interface/contracts. – Zorgiev Jun 19 '14 at 10:45
  • Precompiled header increases build speed if you have multiple translation units(multiple of ten) that include the very same precompiled header. Just consider MSVC stdafx.h approach. it's being included across all the translation units within the same very project. – Zorgiev Jun 19 '14 at 10:51
  • Regarding extern templates I'm not wrong. C++11 fixed this issue. In your header file you specify generic template version. In some translation unit you include this header and make: `template class MyClass;` This instructs compiler that you want to instantiate template explicitly here. Next in your header file with your template definition you specify: `extern template class MyClass;` This informs compiler that encounters this extern template that the instantiation is already available elsewhere. And so the linker will resolve this instantiation lookup. – Zorgiev Jun 19 '14 at 11:15