title: How to force gcc to link an unused static library
body: <p>I have a program and a static library:</p>
  
  <pre><code>// main.cpp
  int main() {}
  
  // mylib.cpp
  #include &lt;iostream&gt;
  struct S {
      S() { std::cout &lt;&lt; "Hello World\n";}
  };
  S s;
  </code></pre>
  
  <p>I want to link the static library (<code>libmylib.a</code>) to the program object (<code>main.o</code>), although the latter does not use any symbol of the former directly.</p>
  
  <p>The following commands do not seem to the job with <code>g++ 4.7</code>. They will run without any errors or warnings, but apparently <code>libmylib.a</code> will not be linked:</p>
  
  <pre><code>g++ -o program main.o -Wl,--no-as-needed /path/to/libmylib.a
  </code></pre>
  
  <p>or</p>
  
  <pre><code>g++ -o program main.o -L/path/to/ -Wl,--no-as-needed -lmylib
  </code></pre>
  
  <p>Do you have any better ideas?</p>