6

I'm working on a large project that must builds under multiple environment, chiefly linux/gcc and windows/msvc. To speed up the build, we use precompiled headers.

The Windows implementation is very efficient: on my quad-core hyperthreaded i7 build time goes down from 9 minutes to 1.5 minutes. However using precompiled headers doesn't seem to improve performance: in both cases it builds in 22 minutes under a virtual box on the same computer, or about 40 minutes on a real server.

So I'm thinking the obvious, that I somehow got something wrong and that the precompiled header isn't kicking in. I can't find what however.

Our Makefiles are generated by CMake, so I can copy paste the code used to compile the header and the object files that uses them.

Creating the header:

/usr/bin/c++ -O3 -DNDEBUG --no-warnings "-I/mnt/code/server a/src/game"
"-I/mnt/code/server a/src/game/vmap" "-I/mnt/code/server a/dep/include/g3dlite"
"-I/mnt/code/server a/dep/include" "-I/mnt/code/server a/src/shared"
"-I/mnt/code/server a/src/framework" "-I/mnt/code/server a/buildlinux"
"-I/mnt/code/server a/buildlinux/src/shared" -I/usr/include/mysql
"-I/mnt/code/server a/dep/acelite" -DDO_MYSQL -DHAVE_CONFIG_H
-DVERSION=\"0.6.1\" -DSYSCONFDIR=\"../etc/\" -D_RELEASE -D_NDEBUG -x c++-header
-o "/mnt/code/server a/buildlinux/src/game/pchdef.h.gch" "/mnt/code/server
a/src/game/pchdef.h"

Compiling an object file:

/usr/bin/c++ $(CXX_DEFINES) $(CXX_FLAGS) "-I/mnt/code/server
a/buildlinux/src/game" -include pchdef.h -Winvalid-pch -o
CMakeFiles/game.dir/AccountMgr.cpp.o -c "/mnt/code/server
a/src/game/AccountMgr.cpp"

Insights are appreciated, even if they don't directly derive from the snippets above.

Norswap
  • 11,740
  • 12
  • 47
  • 60
  • 1
    You are lucky, i found that using precompiled headers with GCC even slows down the build. CLANG was better some time ago but now it seems to be worthless using precompiled headers, leaving MSVC as the only compiler which benefits from it. – Lothar Jun 16 '15 at 21:13

1 Answers1

1

There are a couple of things that you need to pay attention to when using precompiled headers in GCC. First of all, the precompiled header must be created with the same arguments as the cpp files are being compiled. Also I assume you have actually included the precompiled header in AccountMgr.cpp?

Try to compile with the -H flag, this will output which include files are being considered. Check that the pchdef-file is mentioned, and see what other include files are being parsed. To have gcc complain about invalid PCH files, consider using -Winvalid-pch.

Braden Steffaniak
  • 2,053
  • 3
  • 25
  • 37
dseifert
  • 1,318
  • 9
  • 22
  • If you look at the commands, you'll see I'm already using `-Winvalid-pch`. Also I don't include the header file in `AccountMgr.cpp`, but the header gets included with the `-include` option. I'll try using the `-H` flag and report back. – Norswap Sep 24 '12 at 09:02
  • I ran the thing with the "-H" flag, it definitely sees and uses the precompiled header. The mystery thickens. – Norswap Sep 25 '12 at 22:17
  • By "uses" you mean that the list of include files displayed shows that pre-compiled include files are not included again? I.e. try to run -H once with -include and once without. If the output is the same, then either your PCH does not include anything relevant (or AccountMgr.cpp is not using a lot of includes) and you just waste time using the PCH, or it is a new clue. – dseifert Sep 26 '12 at 06:08
  • I finally got around to trying it. The result is exactly what would be expected: the headers included in the precompiled header do not show up with -H when -include is used and they are being included indirectly, but show up when -include isn't used. – Norswap Oct 02 '12 at 12:48
  • 1
    @Norswap, did you find the problem? Recently I enabled PCH support for a project (g++ v4.6), however I can see zero speedup even though `.gch` files are used by the compiler. – Alex Blekhman Dec 13 '12 at 12:21
  • Precompiling is just useless on gcc and now even clang doesn't see speed improvements anymore. We all need to wait for C++20 modules or use MSVC to see how it's done correctly (compiling multiple files at once). – Lothar Sep 01 '17 at 21:13