TemplateRex
TemplateRex

Reputation: 70526

Boost.Program_options not linking correctly under Clang

The following initial example from the Boost.Program_options documentation

// 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;
}

compiles, links and runs correctly under g++ (live example), but not under clang (live example) with error

/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)

Question: what gives?

Upvotes: 4

Views: 1464

Answers (1)

rhashimoto
rhashimoto

Reputation: 15841

The GCC C++ ABI changed in version 5 which can cause some object incompatibilities:

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

I think it is likely that your version of boost has been built using GCC 5 (CoLiRu has 5.2 installed), and the resulting libraries are not compatible with clang++ objects.

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

Upvotes: 3

Related Questions