How to generate .pch for a large number of headers?

My code uses libcxx, sdl and some other libraries all the time. How can I generate .pch, given each title, may include some other titles (even with difficult conditions like #ifdef #include #endif. Therefore, itโ€™s difficult to understand the necessary list of header files. Should I just use only all header files found in what folders to create .pch? How about using such .pch performance in this case?

UPDATE: if this is important, I am going to use it with Clang (not GCC) and more specifically using the Clang C API. UPDATE2:

I created pch for one header file:

MBA-Anton:pch asmirnov$ clang++ -x c++-header header.h -emit-pch -o header.pch MBA-Anton:pch asmirnov$ clang++ -include-pch header.pch source.cpp -o source -x c++ 

but I could not create pch for multiple files:

 MBA-Anton:pch asmirnov$ clang++ -x c++-header header.h header2.h -emit-pch -o headers.pch clang: error: cannot specify -o when generating multiple output files 
+6
source share
2 answers

The GCC component only works well with one included precompiled header as explained in this answer . Therefore, if you want to use PCH in GCC, you need to have one header file for all translation units of your applications (and each of them should definitely start with this single #include directive)

I suppose this situation is also useful for other compilers supporting some precompiled headers.

Future versions (post C ++ 14 ) of the C ++ standard may define a module mechanism. See n4047 .

Note that precompiled headers are very specific to the compiler (with GCC, they may not even work when crawling from GCC 4.9.1 to GCC 4.9.2), so you should not depend on them too much.

+2
source

Firstly, I did not use pch with gcc only with microsoft compiler.

#Ifdef conditions, etc. all are allowed before compilation, so if you change the preprocessor variable, the PCH file will be compiled. So just add all the files from the library that you need.

Generally speaking, include all headers that are unlikely to change often (i.e. external libraries and parts of your system that are stable).

Don't worry about including headers several times in your pch file. Since this file will not be compiled very often, you can ignore the long compilation time here.

What I'm not sure is if the execcutable file size increases when you include everything in your pch file. This may be because you are referencing other unused parts of the code that were compiled into your executable file only because you added them to your pch file. You can check it out.

Now, in my experience, another important question: if your compilation is slow when compiling or when linking.

When you have time with a high link, the problem is more likely that you include too many headers in the headers of your code.

This can be optimized by using Forward declarations in header files and including only headers in .cpp files. We had huge monolithic projects, and each time the greatest increase in performance for compilation / linking was by removing header dependencies by declaring as much as possible.

This is because incremental compilation works very well, but incremental compilation does not work.

Here is a brief but good collection of do and don'ts regarding forward declarations: http://www.chromium.org/developers/coding-style/cpp-dos-and-donts

+1
source

Source: https://habr.com/ru/post/977695/


All Articles