smallB
smallB

Reputation: 17120

How to create precompiled header with codeblocks and gcc

I have a file which has been tested and works as intended:

#ifndef PROMOTE_H_INCLUDED
#define PROMOTE_H_INCLUDED
#include <boost/mpl/vector.hpp>
#include <boost/mpl/find.hpp>
#include <boost/mpl/next.hpp>
#include <boost/mpl/deref.hpp>


template<class Integral>
struct Promote
{
    typedef  boost::mpl::vector<char,short,int,long,long long> types;
    typedef typename boost::mpl::find<types,Integral>::type this_type;
    typedef typename boost:: mpl::next<this_type>::type next_type;
    typedef typename boost::mpl::deref<next_type>::type type;

};
#endif // PROMOTE_H_INCLUDED  

Every time I change something in my project this file is being compiled over and over again which is bit sill. I tried to search net and I found:
http://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html But to be honest I just don't see anywhere there instruction how to create precompiled header. So could anyone, step by step tell me how to do it using code::blocks?
thanks.

Upvotes: 2

Views: 2672

Answers (2)

user2534096
user2534096

Reputation:

The -x and -c options are not needed when compiling a .h file. (The -o option is also generally not needed, unless for example, the header name doesn't correspond to the desired precompiled header name.) Note that the gcc docs state to use the -x option to cause the compiler to treat the input file as a header file - but - only 'if needed". For a .h file, the -x is not needed. g++ knows what to do with a .h file as input file. Because a .h file is a header file, g++ will not compile it to an object, so -c and -o are not needed. The entire purpose (AFAIK) of g++ allowing compiling of a .h, is indeed to create a precompiled header (.h.gch) .

Simply use g++ pch.h to create pch.h.gch. (I have tested this with gcc 10.2) GCC Precompile

A .cpp may also be used, in which case you do use the -x option to cause the .cpp file to be compiled as a header file. E.g. g++ -x c++-header -c pch.cpp -o pch.h.gch (I have tested this with gcc 10.2)

With cpp files that include pch.h, the -H compile option can be used to ensure that the precompiled is being consumed properly. E.g. g++ -H ExampleSrcFile.cpp where #include "pch.h" is the first statement in the ExampleSrcFile.cpp.

Upvotes: 0

Mat
Mat

Reputation: 206689

From the docs you link:

To create a precompiled header file, simply compile it as you would any other file, if necessary using the -x option to make the driver treat it as a C or C++ header file.

So:

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

for C++ code will create the precompiled header.

It wont speed up the build process in the way you seem to want it though. If you change that header, you'll need to update the precompiled header too, and all its dependencies.

Upvotes: 1

Related Questions