I was having trouble preparing the finished headers, so I came up with the following minimal working example.
This is the header file foo.h
#include <iostream> using namespace std; void hello() { cout << "Hello World" << endl; }
I will compile this since g++ -c foo.h gives me the compiled header foo.gch . I expect that when I compile the following source file including foo.h , it should select the header foo.h.gch , and I'm good.
// test.cpp #include <cstdio> // Swap ordering later #include "foo.h" // ------------------ int main() { hello(); }
But surprisingly, this does not compile using foo.h.gch , but uses foo.h To check, you can compile this as g++ -H test.cpp
However, if I reorder the included header files as follows:
// test.cpp #include "foo.h" // ------------------ #include <cstdio> // Ordering swapped int main() { hello(); }
Now, if I compile g++ -H test.cpp using g++ -H test.cpp , it compiles from foo.h.gch , whew!
So, I was wondering if this is a bug in GCC or should we use precompiled headers? In any case, I think this is useful to know.
c ++ gcc precompiled-headers
sud03r Nov 15 '16 at 21:09 2016-11-15 21:09
source share