title: Speed up extremely slow MinGW-w64 compilation/linking?
body: <p>How can I speed up MinGW-w64's <em>extremely</em> slow C++ compilation/linking?</p>
  
  <p>Compiling a trivial "Hello World" program:</p>
  
  <pre><code>#include &lt;iostream&gt;
  int main()
  {
      std::cout &lt;&lt; "hello world" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>...takes <em>3 minutes</em>(!) on this otherwise-unloaded Windows 10 box (<a href="https://ark.intel.com/content/www/us/en/ark/products/88196/intel-core-i7-6700-processor-8m-cache-up-to-4-00-ghz.html" rel="nofollow noreferrer">i7-6700</a>, 32GB of RAM, decent SATA SSD):</p>
  
  <pre><code>&gt; ptime.exe g++ main.cpp
  
  ptime 1.0 for Win32, Freeware - http://www.pc-tools.net/
  Copyright(C) 2002, Jem Berkes &lt;jberkes@pc-tools.net&gt;
  
  ===  g++ main.cpp ===
  
  Execution time: 180.488 s
  </code></pre>
  
  <p><a href="https://docs.microsoft.com/en-us/sysinternals/downloads/process-explorer" rel="nofollow noreferrer">Process Explorer</a> shows the <code>g++</code> process tree bottoming out in <code>ld.exe</code> which doesn't use any appreciable CPU or I/O for the duration.</p>
  
  <p>Running the <code>g++</code> process tree through <a href="http://www.rohitab.com/apimonitor" rel="nofollow noreferrer">API Monitor</a> shows there are three unusually long syscalls in <code>ld.exe</code>: two <code>NtCreateFile()</code>s and a <code>NtOpenFile()</code>, each operating on <code>a.exe</code> and taking 60 seconds apiece.</p>
  
  <p>The slowness only happens when using the default <code>a.exe</code> output; <code>g++ -o foo.exe main.cpp</code> takes 2 seconds, tops.</p>
  
  <p>"Well don't use <code>a.exe</code> as an output name then!" isn't really a solution since this behavior causes CMake to take ages doing compiler feature detection.</p>
  
  <p>GCC toolchain versions:</p>
  
  <pre><code>&gt;g++ --version
  g++ (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
  
  &gt;ld --version
  GNU ld (GNU Binutils) 2.30
  </code></pre>