<p>How can I get precompiled headers working with GCC?</p>
<p>I have had no luck in my attempts and I haven't seen many good examples for how to set it up.  I've tried on <a href="https://en.wikipedia.org/wiki/Cygwin" rel="nofollow noreferrer">Cygwin</a> GCC 3.4.4 and using 4.0 on <a href="https://en.wikipedia.org/wiki/Ubuntu_%28operating_system%29" rel="nofollow noreferrer">Ubuntu</a>.</p>

[{'answer_id': 58987, 'author': 'Paul', 'author_id': 5841, 'author_profile': 'https://Stackoverflow.com/users/5841', 'pm_score': 3, 'selected': False, 'text': '<p>I have managed to get precompiled headers working under gcc once in the past, and I recall having problems then as well. The thing to remember is 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 <a href="http://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html" rel="noreferrer">gcc precompiled header documentation page</a>.</p>\n\n<p>Generally it\'s safest to have your build system compile the .gch file as a first step, with the same command line options and executable as the rest of your source. This ensures the file is up to date and that there are no subtle differences.</p>\n\n<p>It\'s probably also a good idea to get it working with a contrived example first, just to remove the possibility that your problems are specific to source code in your project.</p>\n'}, {'answer_id': 60577, 'author': 'simon', 'author_id': 4875, 'author_profile': 'https://Stackoverflow.com/users/4875', 'pm_score': 3, 'selected': False, 'text': '<p>Call GCC the same way as if you call it for your source file, but with a header file.</p>\n<p>E.g.,</p>\n<pre><code>g++ $(CPPFLAGS) test.h\n</code></pre>\n<p>This generates a file called <em>test.h.gch</em>.</p>\n<p>Every time GCC searches for <em>test.h</em>, it looks first for <em>test.h.gch</em> and if it finds it it uses it automatically.</p>\n<p>More information can be found under <a href="http://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html" rel="nofollow noreferrer">GCC Precompiled Headers</a>.</p>\n'}, {'answer_id': 1191407, 'author': 'Brian R. Bondy', 'author_id': 3153, 'author_profile': 'https://Stackoverflow.com/users/3153', 'pm_score': 6, 'selected': False, 'text': '<p><a href="http://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html" rel="noreferrer">Firstly, see the documentation here</a>.</p>\n\n<p>You compile headers just like any other file but you put the output inside a file with a suffix of <code>.gch</code>.  </p>\n\n<p>So for example if you precompile stdafx.h you will have a precompiled header that will be automatically searched for called <code>stdafx.h.gch</code> anytime you include <code>stdafx.h</code></p>\n\n<p>Example:</p>\n\n<p>stdafx.h:</p>\n\n<pre><code>#include &lt;string&gt;\n#include &lt;stdio.h&gt;\n</code></pre>\n\n<p>a.cpp:</p>\n\n<pre><code>#include "stdafx.h"\nint main(int argc, char**argv)\n{\n  std::string s = "Hi";\n  return 0;\n}\n</code></pre>\n\n<p>Then compile as:</p>\n\n<blockquote>\n  <p><code>&gt; g++ -c stdafx.h -o stdafx.h.gch</code><br>\n  <code>&gt; g++ a.cpp</code><br>\n  <code>&gt; ./a.out</code></p>\n</blockquote>\n\n<p>Your compilation will work even if you remove stdafx.h after step 1.</p>\n'}, {'answer_id': 2935536, 'author': 'User1', 'author_id': 125380, 'author_profile': 'https://Stackoverflow.com/users/125380', 'pm_score': 6, 'selected': False, 'text': '<p>I have definitely had success.  First, I used the following code:</p>\n<pre class="lang-cpp prettyprint-override"><code>#include &lt;boost/xpressive/xpressive.hpp&gt;\n#include &lt;iostream&gt;\n\nusing namespace std;\nusing namespace boost::xpressive;\n\n// A simple regular expression test\nint main()\n{\n    std::string hello(&quot;Hello, World!&quot;);\n\n    sregex rex = sregex::compile( &quot;(\\\\w+) (\\\\w+)!&quot; );\n    smatch what;\n\n    if( regex_match( hello, what, rex ) )\n    {\n        std::cout &lt;&lt; what[0] &lt;&lt; \'\\n\'; // Whole match\n        std::cout &lt;&lt; what[1] &lt;&lt; \'\\n\'; // First capture\n        std::cout &lt;&lt; what[2] &lt;&lt; \'\\n\'; // Second capture\n    }\n    return 0;\n}\n</code></pre>\n<p>This was just a <em><a href="https://en.wikipedia.org/wiki/%22Hello,_World!%22_program" rel="nofollow noreferrer">Hello, World!</a></em> program <a href="http://www.boost.org/doc/libs/1_43_0/doc/html/xpressive/user_s_guide.html#boost_xpressive.user_s_guide.examples" rel="nofollow noreferrer">from Boost Xpressive</a>.  First, I compiled with the <code>-H</code> option in GCC.  It showed an enormous list of headers that it used.  Then, I took a look at the compile flags my IDE (<a href="https://en.wikipedia.org/wiki/Code::Blocks" rel="nofollow noreferrer">Code::Blocks</a>) was producing and saw something like this:</p>\n<pre class="lang-none prettyprint-override"><code>g++ -Wall -fexceptions  -g  -c main.cpp -o obj/Debug/main.o\n</code></pre>\n<p>So I wrote a command to compile the <em>Xpressive.hpp</em> file with the exact same flags:</p>\n<pre class="lang-none prettyprint-override"><code>sudo g++ -Wall -fexceptions  -g /usr/local/include/boost/xpressive/xpressive.hpp\n</code></pre>\n<p>I compiled the original code again with the <code>-H</code> and got this output:</p>\n<pre class="lang-none prettyprint-override"><code>g++ -Wall -fexceptions -H  -g     -c main.cpp -o obj/Debug/main.o\n\n! /usr/local/include/boost/xpressive/xpressive.hpp.gch\nmain.cpp\n. /usr/include/c++/4.4/iostream\n.. /usr/include/c++/4.4/x86_64-linux-gnu/bits/c++config.h\n.. /usr/include/c++/4.4/ostream\n.. /usr/include/c++/4.4/istream\nmain.cpp\n</code></pre>\n<p>The ! means that the compiler was able to use the precompiled header.  An x means it was not able to use it.  Using the appropriate compiler flags is crucial.  I took off the -H and ran some speed tests.  The precompiled header had an improvement from 14 seconds to 11 seconds.  Not bad, but not great.</p>\n<p>Note: Here\'s <a href="http://www.boost.org/doc/libs/1_43_0/doc/html/xpressive/user_s_guide.html#boost_xpressive.user_s_guide.examples" rel="nofollow noreferrer">the example</a>. I couldn\'t get it to work in the post.</p>\n<p>BTW: I\'m using the following <em>g++</em>:</p>\n<pre class="lang-none prettyprint-override"><code>g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3\n</code></pre>\n'}, {'answer_id': 10756166, 'author': 'psaghelyi', 'author_id': 315527, 'author_profile': 'https://Stackoverflow.com/users/315527', 'pm_score': 3, 'selected': False, 'text': '<p>The <code>-x</code> specifier for C++ precompiled headers is <code>-x c++-header</code>, not <code>-x c++</code>. Example usage of PCH follows.</p>\n\n<p><code>pch.h</code>:</p>\n\n<pre><code>// Put your common include files here: Boost, STL as well as your project\'s headers.\n</code></pre>\n\n<p><code>main.cpp</code>:</p>\n\n<pre><code>#include "pch.h"\n// Use the PCH here.\n</code></pre>\n\n<p>Generate the PCH like this:</p>\n\n<pre class="lang-sh prettyprint-override"><code>$ g++ -x c++-header -o pch.h.gch -c pch.h\n</code></pre>\n\n<p>The <code>pch.h.gch</code> must be in the same directory as the <code>pch.h</code> in order to be used, so make sure that you execute the above command from the directory where <code>pch.h</code> is.</p>\n'}, {'answer_id': 60571708, 'author': 'Íhor Mé', 'author_id': 2617351, 'author_profile': 'https://Stackoverflow.com/users/2617351', 'pm_score': 2, 'selected': False, 'text': '<p><em>Make sure to <code>-include your_header.h</code></em></p>\n<p>This is how I precompiled and used <code>bits/stdc++.h</code> collection.</p>\n<p>Code</p>\n<pre><code>#include &lt;bits/stdc++.h&gt;\n</code></pre>\n<p>Then I located the lib by compiling my file with -H and looking at output</p>\n<pre class="lang-none prettyprint-override"><code>g++ sol.cpp -H -O3 -pthread -lm -std=c++14 -o executable\n</code></pre>\n<p>where I saw</p>\n<pre class="lang-none prettyprint-override"><code>. /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h\n</code></pre>\n<p>So I made a new directory <code>bits</code> inside of current one and copied <code>stdc++.h</code> from there.</p>\n<p>Then I ran</p>\n<pre class="lang-none prettyprint-override"><code>g++ bits/stdc++.h -O3 -std=c++14  -pthread\n</code></pre>\n<p>which generated <code>bits/stdc++.gch</code></p>\n<p>Normally I compiled my code via</p>\n<pre class="lang-none prettyprint-override"><code>g++ sol.cpp -O3 -pthread -lm -std=c++14 -o executable\n</code></pre>\n<p>, but I had to modify that to</p>\n<pre class="lang-none prettyprint-override"><code>g++ sol.cpp -include bits/stdc++.h -O3 -pthread -lm -std=c++14 -o executable\n</code></pre>\n<p>as it only resolved to <code>.gch</code> file instead of <code>.h</code> with <strong><code>-include bits/stdc++.h</code></strong>\nThat was key for me. Other thing to keep in mind is that you have to compile <code>*.h</code> header file with almost the same parameters as you compile your <code>*.cpp</code>. When I didn\'t include <code>-O3</code> or <code>-pthread</code> it ignored the <code>*.gch</code> precompiled header.</p>\n<p>To check if everything\'s correct you can measure time difference via comparing result of</p>\n<pre class="lang-none prettyprint-override"><code>time g++ sol.cpp ...\n</code></pre>\n<p>or run</p>\n<pre class="lang-none prettyprint-override"><code>g++ sol.cpp -H -O3 -pthread -lm -std=c++14 -o executable\n</code></pre>\n<p>again and look for header paths and if you now get <code>!</code> before library path, for example</p>\n<pre class="lang-none prettyprint-override"><code>! ./bits/stdc++.h.gch\n....\n</code></pre>\n'}, {'answer_id': 65730829, 'author': 'Brad Robinson', 'author_id': 77002, 'author_profile': 'https://Stackoverflow.com/users/77002', 'pm_score': 0, 'selected': False, 'text': "<p>A subtle tip about the file extension that tripped me up, because I wasn't paying close enough attention:  the <code>.gch</code> extension is added to the precompiled file's full name; it doesn't replace <code>.h</code>. If you get it wrong, the compiler won't find it and silently does not work.</p>\n<p>precomp.h =&gt; precomp.<strong>h.gch</strong></p>\n<p>Not:</p>\n<p>precomp.h =&gt; precomp.<strong>gch</strong></p>\n<p>Use GCC's <code>-H</code> to check if it's finding/using it.</p>\n"}]
['https://Stackoverflow.com/questions/58841', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5200/']