title: undefined reference cross compiling static libraries with LTO under GCC
body: <p>I am attempting to use GCC 4.9.2 to cross compile an application from Linux (x86_64-pc-linux-gnu) for Windows (x86_64-w64-mingw32).</p>
  
  <p>When building targets that link against static libraries and also using link-time optimisation I get undefined reference errors from the linker for all symbols the target uses from the library.</p>
  
  <p>eg, building bar.a from bar.cpp</p>
  
  <pre><code>int bar (void) {return 42;}
  </code></pre>
  
  <p>and linking with foo.cpp</p>
  
  <pre><code>extern int bar (void);
  int main (int, char**) {bar ();}
  </code></pre>
  
  <p>using the command line</p>
  
  <pre><code>x86_64-w64-mingw32-g++ -flto -o foo.o -c foo.cpp
  x86_64-w64-mingw32-g++ -flto -o bar.o -c bar.cpp
  x86_64-w64-mingw32-gcc-ar rc bar.a bar.o
  x86_64-w64-mingw32-gcc-ranlib bar.a
  x86_64-w64-mingw32-g++ -flto -fuse-linker-plugin foo.o bar.a -o foo
  </code></pre>
  
  <p>Results in the error</p>
  
  <pre><code>/tmp/ccc3Twsc.lto.o:foo.o:(.text+0x15): undefined reference to `bar()'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>From above:</p>
  
  <ul>
  <li>I am using the gcc-wrappers for ar/ranlib</li>
  <li>there are no external dependencies</li>
  <li>all files are compiled with the same options</li>
  </ul>
  
  <p>I have tried using various combinations of -fuse-linker-plugin, gcc-ar vs ar, symbol visibility options, optimsations, etc but I can't get it to link correctly without turning off LTO.</p>
  
  <p>All targets build correctly under the native compiler (x86_64 Linux).</p>
  
  <p>Is there something obvious I'm missing here?</p>