Slow Compile Time with Boost + GCC + precompiled header
boost, c++, compilation, gcc, precompiled
Solution
GCC's precompiled headers work in a very particular way. Only one precompiled header file can be used in any given source file. Use `-H` to show whether a given header file is using a precompiled version or not.
Moreover, you must compile the header file with the exact same compiler flags as the source file that uses it.
The typical way to set up a PCH environment is like this:
main.cpp:
#include "allheaders.hpp"
int main() { /* ... */ }
allheaders.hpp:
#include <algorithm>
// ... everything you need
Compilation:
g++ $CXXFLAGS allheaders.hpp # 1
g++ $CXXFLAGS -H -c -o main.o main.cpp # 2
g++ $LDFLAGS -o myprogram main.o # 3
After Step #1 you should end up with a file `allheaders.hpp.gch`, which should be pretty big. In Step #2 the `-H` flag should produce additional output that tells you that the precompiled header file is being used. Step #3 links the executable.
The idea is that Step #1 can potentially take a very long time, but Step #2 should become a lot faster.
Problem
Running: gcc version 4.2.1 (Apple Inc. build 5664) I created an apple XCode project with a default precompiled header. It appears to be very slow, and a trivial main file with a main function no includes no code takes 6 seconds to compile, which is after I upgraded to a new SSD drive. I am on a laptop, but I have reservations that upgrading to a workstation would alleviate my problem. If I turn off the precompiled header then the main file compiles in under a second. It appears that using a precompiled header puts a penalty across all files. This delay is makes me want to avoid compiling and experimenting with code which is not good. Here is what I am including in my precompiled header: ``` #pragma once #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <fstream> #include <functional> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <valarray> #include <vector> #include <boost/smart_ptr/scoped_ptr.hpp> #include <boost/smart_ptr/scoped_array.hpp> #include <boost/smart_ptr/shared_ptr.hpp> #include <boost/smart_ptr/shared_array.hpp> #include <boost/smart_ptr/make_shared.hpp> #include <boost/smart_ptr/weak_ptr.hpp> #include <boost/smart_ptr/intrusive_ptr.hpp> #include <boost/regex.hpp> #include <boost/thread.hpp> #include <boost/bind/bind.hpp> #include <boost/bind/apply.hpp> #include <boost/bind/protect.hpp> #include <boost/bind/make_adaptable.hpp> #include <boost/asio.hpp> //#include <boost/asio/ssl.hpp> #include <boost/property_tree/ptree.hpp> #include <boost/random.hpp> #include <boost/lexical_cast.hpp> #include <boost/date_time/gregorian/gregorian.hpp> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/local_time/local_time.hpp> #include <boost/date_time/time_zone_base.hpp> #include <boost/circular_buffer.hpp> #include <boost/accumulators/accumulators.hpp> #include <boost/accumulators/statistics.hpp> ``` I have not included spirit, which really makes the compile time go up.