Boost Program_Options throws "character conversion failed"
Asked Answered
C

3

7

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)

Constance answered 13/2, 2016 at 17:20 Comment(8)
No error for me using g++ 4.9.2 and Boost 1.55.Koheleth
I am using Boost 1.60.0Constance
I have the same problem on a DigitalOcean clean VM. So I suppose there is either a problem with Boost or with my compilation.... But I don't know how to test these theories.Constance
I installed Ubuntu on another computer and it works. Could it be a bug of CMake? (I say this because moving to compiled program from one computer to another does not bring the success)Constance
The bug apperas also in boost 1.59 on Ubuntu 15.04 3.19.0-51-genericUnpaid
Looks like there is a Boost ticket for this: svn.boost.org/trac/boost/ticket/11905. I ran into the same problem with Boost 1.58 on Ubuntu 15.04. It worked with Boost 1.57.Shericesheridan
I have the same problem with Boost 1.51.0 and Ubuntu 16.04.1 (64 bit)Apodal
I had the same problem with Boost 1.58 on Ubuntu 16.04 (LTS). I downgraded to Boost 1.57 and it worked.Col
B
5

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.

Bergama answered 18/8, 2016 at 2:23 Comment(0)
W
1

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!

Whereby answered 19/7, 2016 at 17:47 Comment(0)
S
-1

I have this problem too,finally I find root cause of my problem,maybe it would helps for you,

when gdb the core file,it shows like this

#4  0x0000000000409ad6 in boost::detail::sp_counted_base::release (this=0x2669970)
    at /usr/include/boost/smart_ptr/detail/sp_counted_base_gcc_x86.hpp:146
#5  0x0000000000411914 in ~shared_count (this=0x266a0d8, __in_chrg=<optimized out>)
    at /usr/include/boost/smart_ptr/detail/shared_count.hpp:371
#6  ~shared_ptr (this=0x266a0d0, __in_chrg=<optimized out>) at /usr/include/boost/smart_ptr/shared_ptr.hpp:328
#7  _Destroy<boost::shared_ptr<boost::program_options::option_description> > (__pointer=0x266a0d0)
    at /usr/include/c++/4.8.2/bits/stl_construct.h:93
#8  __destroy<boost::shared_ptr<boost::program_options::option_description>*> (__last=<optimized out>, 
    __first=0x266a0d0) at /usr/include/c++/4.8.2/bits/stl_construct.h:103
#9  _Destroy<boost::shared_ptr<boost::program_options::option_description>*> (__last=<optimized out>, 
    __first=<optimized out>) at /usr/include/c++/4.8.2/bits/stl_construct.h:126

I found it use the system include file when I compile the exe file, but it link the boost.a file which is not the same version as system boost. it's suprised. when i remove the system boost,all is ok!

Saritasarkaria answered 14/3, 2017 at 5:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.