Boost Program Options won't work with GLIBCXX_

2019-04-21 07:06发布

问题:

I have the following sample code:

#include <iostream>
#include <boost/program_options.hpp>

int main ( int ac, char *av[] ) {
    // Declare the supported options.
    boost::program_options::options_description desc("Allowed options");
    desc.add_options()("help", "produce help message");

    boost::program_options::variables_map vm;
    boost::program_options::store(boost::program_options::parse_command_line(ac, av, desc), vm);
    return 0;
}

It compiles fine using e.g. g++ test.cpp -lboost_program_options. However if I try to activate GCC bounds checking with the call g++ test.cpp -lboost_program_options -D_GLIBCXX_DEBUG, it throws the following linker error:

/tmp/ccZLdZ1g.o: In function `boost::program_options::basic_command_line_parser<char>::basic_command_line_parser(int, char const* const*)':
test.cpp:(.text._ZN5boost15program_options25basic_command_line_parserIcEC2EiPKPKc[_ZN5boost15program_options25basic_command_line_parserIcEC5EiPKPKc]+0x97): undefined reference to `boost::program_options::detail::cmdline::cmdline(std::__debug::vector<std::string, std::allocator<std::string> > const&)'
collect2: error: ld returned 1 exit status

As far as I understand the linker can't find the function boost::program_options::detail::cmdline::cmdline(std::__debug::vector<std::string, std::allocator<std::string> > const&), because its argument is replaced by a debug vector instead of normal std::vector. But why does this happen? And does anyone know a workaround, to make Boost Program Options work with GLIBCXX_DEBUG?

I use the following system:

  • Debian Wheezy
  • g++ (Debian 4.7.2-5) 4.7.2
  • libboost-all-dev 1.49.0.1, installed via aptitude

Thanks for any help

回答1:

The error message is very clear here, the linker cannot find the symbol

boost::program_options::detail::cmdline::cmdline(std::__debug::vector<std::string, std::allocator<std::string> > const&)

note the additional __debug namespace, which implies you are building with _GLIBCXX_DEBUG. This won't work since your package maintainer did not build the boost libraries with this defined, hence the linker error. You have a few options

  • remove _GLIBCXX_DEBUG from whatever translation units include the program options headers. This might require some refactoring depending on what you're trying to solve with iterator debugging enabled.
  • build the boost libraries with -D_GLIBCCX_DEBUG. This also may not be trivial, though the boost build system is fairly straightforward.