Boost.Program_options 在 Clang 下链接不正确

Boost.Program_options not linking correctly under Clang

Boost.Program_options 文档中的以下初始示例

// Copyright Vladimir Prus 2002-2004.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)

/* The simplest usage of the library.
 */

#include <boost/program_options.hpp>
namespace po = boost::program_options;

#include <iostream>
#include <iterator>
using namespace std;

int main(int ac, char* av[])
{
    try {

        po::options_description desc("Allowed options");
        desc.add_options()
            ("help", "produce help message")
            ("compression", po::value<double>(), "set compression level")
        ;

        po::variables_map vm;        
        po::store(po::parse_command_line(ac, av, desc), vm);
        po::notify(vm);    

        if (vm.count("help")) {
            cout << desc << "\n";
            return 0;
        }

        if (vm.count("compression")) {
            cout << "Compression level was set to " 
                 << vm["compression"].as<double>() << ".\n";
        } else {
            cout << "Compression level was not set.\n";
        }
    }
    catch(exception& e) {
        cerr << "error: " << e.what() << "\n";
        return 1;
    }
    catch(...) {
        cerr << "Exception of unknown type!\n";
    }

    return 0;
}

在 g++ (live example), but not under clang (live example) 下编译、链接和运行正确,但有错误

/tmp/main-47ef95.o: In function boost::program_options::typed_value<double, char>::name() const': main.cpp:(.text._ZNK5boost15program_options11typed_valueIdcE4nameEv[_ZNK5boost15program_options11typed_valueIdcE4nameEv]+0x49): undefined reference toboost::program_options::arg' /tmp/main-47ef95.o: In function boost::program_options::validation_error::validation_error(boost::program_options::validation_error::kind_t, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)': main.cpp:(.text._ZN5boost15program_options16validation_errorC2ENS1_6kind_tERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESA_i[_ZN5boost15program_options16validation_errorC2ENS1_6kind_tERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESA_i]+0x39): undefined reference to boost::program_options::validation_error::get_template(boost::program_options::validation_error::kind_t)' clang: error: linker command failed with exit code 1 (use -v to see invocation)

问题:给出了什么?

可能导致某些对象不兼容的GCC C++ ABI changed in version 5

Users just need to make sure that they are building with the ABI used by the libraries that they depend on.

我认为您的 boost 版本可能是使用 GCC 5 构建的(CoLiRu 安装了 5.2),并且生成的库与 clang++ 对象不兼容。

This blog post discusses GCC5 and Clang compatibility, and links to an open LLVM bug to restore ABI interop with GCC.