Boost Program_Options throws "unsuccessful character conversion"

I'm 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 does this not work and how can I solve it?

EDIT: After some debugging, I found that the problem comes from the line with store , if that helps you. Also, I should mention that I tried to use store(..., true) (setting unicode to true )

+5
source share
3 answers

I ran into the same problem, going from 1.58 to 1.61 .
My problem was that I linked the 1.61 boost header code with the old 1.58 shared libraries.

You might have installed a newer version of boost, but that doesn’t mean you are still not linking to the old boost libraries. Check your linker. Check system files.
A good test you can do in your program is to run it through gdb , minimize it and look at backtrace (bt). It will show the version numbers for the accelerated version. See if this is as expected.

You mentioned Ubuntu, and that is what I, too. I built boost from the source like this:

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

This caused my library files to be located in /usr/lib/libboost* .
However, my linker looked at /usr/lib/x86_64-linux-gnu/libboost* .

A simple cp -Pf on old files solved my problem.

+4
source

I ran into the same problem with a very similar piece of code 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 summarize, this problem does not seem to be directly related to the Boost libraries, but seems to be related to the installation of the Boost system. Another SO> question points to a similar problem, and according to the comments, only a successful reinstall of the same version of Boost (1.60 in their case) was also successful.

Hope this helps someone!

+1
source

I also have this problem, in the end, I find the root cause of my problem, maybe this will help you,

when gdb is the main file, it shows it as

 #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 that it uses the system include file when I compile the exe file, but it links the boost.a file, which does not match the version of the system. he was surprised. when I remove the system impulse, everything is fine!

-1
source

Source: https://habr.com/ru/post/1242958/


All Articles