views:

77

answers:

1

I have a really short program written in boost::xpressive


#include <iostream>
#include <boost/xpressive/xpressive.hpp>

using namespace boost::xpressive;

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's the Xpressive "hello world". It takes significantly longer than an ordinary hello world to compile. I think it's because the xpressive.hpp file is so huge. Is there a way to pre-compile or pre-process the .hpp file so the compile will be much faster?

+6  A: 

You can use precompiled headers if your compiler supports them; both g++ and Visual C++ support precompiled headers, as do most other modern C++ compilers, I suspect.

James McNellis
interesting. Is there an easy way to precompile all boost headers in Ubuntu?
User1
@user1. You could but it's a bad idea. A quick search for 'precompiled headers and templates" gives http://gamesfromwithin.com/the-care-and-feeding-of-pre-compiled-headers. Down in the comments someone says that Boost.MPL generates HUGE pch files that made build times increase instead of decrease.
KitsuneYMG
@kts. Thanks for the link. It's a very interesting article. I found the comment you mentioned. The user had VC++. I wonder if the slow down would also happen in gcc.
User1
We use precompiled headers extensively with Visual C++ 2008 and large parts of the Boost library; I can't say we've seen anything like what was described in that comment thread. Generally, precompiling the Boost headers has a net positive impact on build performance.
James McNellis
Why not precompile all headers your application uses? The patters involves creating a single header file called stdafx.h in which you include all you need, and then compile it to get stdafx.h.pch. Now in your .cpp files you include stdafx.h and the compiler will automatically use the .pch (you can use only one precompiled header per application in gcc).
kyku
@kyku: You only want to include headers that don't change on a regular basis (e.g., third party libraries). Otherwise, you end up having to rebuild the pch file every time you change one of your own headers, which would result in a huge hit to your average compile time.
James McNellis