title: How can I use lto with static libraries?
body: <p>When I try to build static libraries with <code>-flto</code>, I get undefined reference errors:</p>
  
  <p><strong>library.cpp</strong>:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  void foo() {
    std::cout &lt;&lt; "Test!" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p><strong>main.cpp</strong>:</p>
  
  <pre><code>void foo();
  
  int main() {
    foo();
    return 0;
  }
  </code></pre>
  
  <p><strong>Compilation output</strong>:</p>
  
  <pre class="lang-none prettyprint-override"><code>$ g++ -flto -c library.cpp
  $ ar rcs library.a library.o
  $ g++ -flto main.cpp library.a
  /tmp/ccZIgxCY.ltrans0.ltrans.o: In function `main':
  ccZIgxCY.ltrans0.o:(.text+0x5): undefined reference to `foo()'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>It works fine if I link with <code>library.o</code> instead of <code>library.a</code>.  What am I missing?  This is with GCC 4.9.1 and binutils 2.24.</p>