Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost Program_Options throws "character conversion failed"

I am on Ubuntu 14.04, using CMake and CLion. I am trying to use Program Options, with the following code taken from an example in their documentation:

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

int main(int ac, char* av[]) {
    namespace po = boost::program_options;
    using namespace std;

    po::options_description desc("Allowed options");
    desc.add_options()
            ("help", "produce help message")
            ("compression", po::value<int>(), "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 1;
    }

    if (vm.count("compression")) {
        cout << "Compression level was set to "
        << vm["compression"].as<int>() << ".\n";
    } else {
        cout << "Compression level was not set.\n";
    }
}

When I run it, I get the following output from the terminal:

$ ./bin/webserver --help
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::logic_error> >'
  what():  character conversion failed
Aborted (core dumped)

Why is that not working and how can I solve it?

EDIT: After some debugging, I found that the problem comes from the line with store, if this is of any help for you. Also, I have to mention I tried using store(..., true) (setting unicode to true)

like image 760
Victor Avatar asked Feb 13 '16 17:02

Victor


2 Answers

I ran into the exact same problem transitioning from 1.58 to 1.61.
My problem was that I was linking 1.61 boost header code with old 1.58 shared libraries.

You may have installed a newer version of boost, but that doesn't mean you still aren't linking with old boost libraries. Check your linker. Check your system files.
A good check you can do on your program, is to run it through gdb, have it crash, and look at the backtrace (bt). It will show the boost version numbers in the backtrace. See if it matches what you expected.

You mentioned Ubuntu, and that is what I am on as well. I built boost from source like so:

sudo ./bootstrap.sh --prefix=/usr
sudo ./b2 install threading=multi link=shared

This resulted in my library files being located at /usr/lib/libboost*.
However, my linker was looking in /usr/lib/x86_64-linux-gnu/libboost*.

A simple cp -Pf over the old files solved my problem.

like image 108
Trevor Hickey Avatar answered Oct 01 '22 02:10

Trevor Hickey


I was encountering the exact same issue with a very similar piece of code while using the Program Options library (version 1.58 in my case).

My solution was to simply reinstall Boost (same version), and the problem was solved without any other code modifications or system changes.

To sum up, this issue doesn't seem to be related to the Boost libraries directly, but seems to be due to the system's Boost installation. Another SO question points to a similar issue, and according to the comments just cleanly reinstalling the same version of Boost (1.60 in their case) was also successful.

Hope this can help someone out!

like image 43
Pyves Avatar answered Oct 01 '22 01:10

Pyves