Precompiled headers with GCC

Has anyone managed to get precompiled headers working with GCC? I was not lucky in my attempts, and I did not see many good examples of how to configure it. I tried on cygwin gcc 3.4.4 and used 4.0 on Ubuntu.

+90
c ++ gcc precompiled-headers
Sep 12 '08 at
source share
5 answers

I definitely had success. First, I used the following code:

#include <boost/xpressive/xpressive.hpp> #include <iostream> using namespace std; using namespace boost::xpressive; //A simple regex test int main() { std::string hello( "hello world!" ); sregex rex = sregex::compile( "(\\w+) (\\w+)!" ); smatch what; if( regex_match( hello, what, rex ) ) { std::cout << what[0] << '\n'; // whole match std::cout << what[1] << '\n'; // first capture std::cout << what[2] << '\n'; // second capture } return 0; } 

It was just a hello world from Boost Xpressive (see link below). Firstly, I compiled with the -H option in gcc. He showed a huge list of titles that he used. Then I looked at the flag compilation that my IDE (code :: blocks) created and saw something like this:

g++ -Wall -fexceptions -g -c main.cpp -o obj/Debug/main.o

So, I wrote a command to compile the Xpressive.hpp file with the same flags:

sudo g++ -Wall -fexceptions -g /usr/local/include/boost/xpressive/xpressive.hpp

I compiled the source code with -H and got this output:

 g ++ -Wall -fexceptions -H -g -c main.cpp -o obj / Debug / main.o
 !  /usr/local/include/boost/xpressive/xpressive.hpp.gch
 main.cpp
 .  /usr/include/c++/4.4/iostream
 .. /usr/include/c++/4.4/x86_64-linux-gnu/bits/c++config.h
 .. /usr/include/c++/4.4/ostream
 .. /usr/include/c++/4.4/istream
 main.cpp

The! means that the compiler was able to use a precompiled header. X means that he could not use it. Using the appropriate compiler flags is crucial. I took off -H and did some speed tests. The precompiled header had an improvement from 14 seconds to 11 seconds. Not bad, but not very.

Note: here is an example link: http://www.boost.org/doc/libs/1_43_0/doc/html/xpressive/user_s_guide.html#boost_xpressive.user_s_guide.examples I couldn’t get it to work in the message.

BTW: I am using the following g ++

g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3

+58
May 29 '10 at 2:59 p.m.
source share

First, see the documentation here .

You compile the headers like any other file, but you put the output in a file with the suffix .gch .

So, for example, if you precompile stdafx.h, you will have a precompiled header that will automatically look for called stdafx.h.gch anytime you turn on stdafx.h

Example:

stdafx.h:

 #include <string> #include <stdio.h> 

a.cpp:

 #include "stdafx.h" int main(int argc, char**argv) { std::string s = "Hi"; return 0; } 

Then compile as:

> g++ -c stdafx.h -o stdafx.h.gch
> g++ a.cpp
> ./a.out

Your compilation will work even if you delete stdafx.h after step 1.

+51
Jul 28 '09 at 0:52
source share

The -x for precompiled C ++ headers is -x c++-header , not -x c++ . An example of using PCH is given below.

pch.h :

 // Put your common include files here: Boost, STL as well as your project headers. 

main.cpp :

 #include "pch.h" // Use the PCH here. 

Create PCH like this:

 $ g++ -x c++-header -o pch.h.gch -c pch.h 

pch.h.gch must be in the same directory as pch.h so that it can be used, so make sure you pch.h above command from the directory where pch.h is located.

+8
May 25 '12 at 14:25
source share

I managed to get precompiled headers running gcc once in the past, and I also remember that I had problems. It should be remembered that gcc will ignore the file (header.h.gch or similar) if certain conditions are not met, a list of which can be found on the gcc precompiled documentation page .

It is generally safest to have your build system compile the .gch file as a first step, with the same command line options and executable files as the rest of your source. This ensures that the file is updated and that there are no subtle differences.

It is probably also a good idea to get it to work with a far-fetched example, first, just to rule out that your problems are specific to the source code in your project.

+6
Sep 12 '08 at 13:40
source share

Call gcc the same way you name it for the source file, but with a header file.

eg,

 g++ $(CPPFLAGS) test.h 

this creates a file called test.h.gch

Each time gcc searches for the test.h file, it first searches for the test.h.gch file and, if it finds it, it uses it automatically.

For more information, see the GCC Precompiled Headers section .

+6
Sep 13 '08 at 14:49
source share



All Articles