6

Precompiled headers in C/C++

 2 years ago
source link: http://codeforces.com/blog/entry/97704
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Precompiled headers in C/C++

By ilia_rr, 9 hours ago,

Hi everybody!

This blog is about speeding up C/C++ compilers with precompiled headers.

Including lots of headers or big headers like bits/stdc++.h will increase compilation time. This can be annoying even with high speed processors. We can solve this using precompiled headers.

What are precompiled headers?

You can compile a header like stdc++.h to stdc++.h.gch and compiler will use it instead of compiling the header every time you are compiling your code. Less processing => Less time.

How to use precompiled headers?

  1. Find your g++ default include directory

    • Ubuntu: /usr/include/x86_64-linux-gnu/c++/{version}
    • Windows: C:\MinGW\lib\gcc\mingw32\{version}\include\c++
  2. Compile headers you need (I recommend using bits/stdc++.h instead of including lots of headers).

    g++ {header name} {flags you use when compiling a normal code}

  3. Put .gch files in the right place. You have 2 Options:

    • Put them in the directory you find the headers & use #include <header name>
    • Put them in your code directory & use #include "header name"

    I recommend first options because the second one will work only when .gch files are in the code directory and it takes a little extra time to search in code.

Compile a code and feel the difference!

Comment your issues or suggestions.

Be good and code fast ;)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK