title: g++ error using -flto option
body: <p>I am trying to enable link time optimization in g++. My program compiles fine without the <code>-flto</code> option. When I add it to my Makefile the object files compile without errors, e.g.</p>
  
  <pre><code>g++ main.cpp -I ../includes -std=c++0x -fopenmp -Wall -pedantic -Wno-vla -flto -D INFO_ -c -o .obj/main.o
  </code></pre>
  
  <p>But when it comes to link the program:</p>
  
  <pre><code>g++ -fwhole-program -I ../includes -std=c++0x -fopenmp -Wall -pedantic -Wno-vla -flto -D INFO_ .obj/main.o .obj/atom.o .obj/bee.o .obj/colony.o ../includes/.obj/error.o ../includes/.obj/CmdLine.o ../includes/boost_lib_deb/libboost_program_options.a ../includes/gmp_lib_deb/lib/libgmpxx.a ../includes/gmp_lib_deb/lib/libgmp.a -o BeeBench
  </code></pre>
  
  <p>I get a lot of errors like these:</p>
  
  <pre><code>includes/gmp_lib_deb/lib/libgmpxx.a ../includes/gmp_lib_deb/lib/libgmp.a -o BeeBench
  `typeinfo for boost::program_options::too_many_positional_options_error' referenced in section `.rodata._ZTVN5boost15program_options33too_many_positional_options_errorE[vtable for boost::program_options::too_many_positional_options_error]' of ../includes/boost_lib_deb/libboost_program_options.a(cmdline.o): defined in discarded section `.gnu.linkonce.t._ZTIN5boost15program_options33too_many_positional_options_errorE' of .obj/main.o (symbol from plugin)
  
  `typeinfo for boost::program_options::too_many_positional_options_error' referenced in section `.rodata._ZTIN5boost16exception_detail19error_info_injectorINS_15program_options33too_many_positional_options_errorEEE[typeinfo for boost::exception_detail::error_info_injector&lt;boost::program_options::too_many_positional_options_error&gt;]' of ../includes/boost_lib_deb/libboost_program_options.a(cmdline.o): defined in discarded section `.gnu.linkonce.t._ZTIN5boost15program_options33too_many_positional_options_errorE' of .obj/main.o (symbol from plugin)
  `typeinfo for boost::program_options::invalid_command_line_style' referenced in section `.rodata._ZTVN5boost15program_options26invalid_command_line_styleE[vtable for boost::program_options::invalid_command_line_style]' of ../includes/boost_lib_deb/libboost_program_options.a(cmdline.o): defined in discarded section `.gnu.linkonce.t._ZTIN5boost15program_options26invalid_command_line_styleE' of .obj/main.o (symbol from plugin)
  </code></pre>
  
  <p>I can't figure out what is going wrong. I compile all my object files using <code>-flto</code>. The libs, namely Boost and GMP, are compiled without <code>-flto</code> option. Is this causing the error? The gcc manual says that its ok to mix object files compiled with &amp; without <code>-flto</code> option. Or am I missing something else, for example what is this plugin the error is speaking about?</p>
  
  <p>I am using G++ 4.6.3 on Debian Wheezy.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>As adviced in the comments I made a minimal example. The code of my test program is only this:</p>
  
  <pre><code>#include "boost/program_options.hpp"
  
  int main ( int argC, char* argV[] )
  {
      return 0;
  }
  </code></pre>
  
  <p>When I compile it using:</p>
  
  <pre><code>g++ -o test -I ../includes -Wall -std=c++0x test.cpp -flto -fwhole-program -static
  </code></pre>
  
  <p>it gives similar errors as described above. If I omit the -static, -flto OR std=c++0x option it compiles without errors. The -fwhole-program option does not change the result. I now also tested with G++ 4.7, same error.</p>
  
  <p>Any suggestions? Is this really a compiler error, or am I still doing something wrong?</p>