2016-02-13 111 views
5

我在Ubuntu 14.04上使用CMake和CLion。我试图使用程序选项,从一个实例在其文档中采取了以下代码:Boost Program_Options抛出“字符转换失败”

#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"; 
    } 
} 

当我运行它,我从终端输出如下:

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

为什么不工作,我该如何解决它?

编辑:经过一些调试后,我发现问题来自store行,如果这对你有任何帮助。此外,我不得不提到我尝试使用store(..., true)(设置unicodetrue

+0

对于使用g ++ 4.9.2和Boost 1.55的我来说没有错误。 – rhashimoto

+0

我正在使用Boost 1.60.0 – Victor

回答

4

我遇到了从1.58过渡到1.61的完全相同的问题。
我的问题是我连接1.61助推头代码与旧的1.58共享库。

您可能已经安装了更新版本的boost,但这并不意味着您仍然没有链接旧的boost库。检查你的链接器。检查你的系统文件。
你可以在你的程序上做一个很好的检查,就是通过gdb运行它,让它崩溃,然后看看回溯(bt)。它会在回溯中显示增强版本号。看看它是否符合你的预期。

你提到过Ubuntu,这也是我所关注的。我内置的升压源像这样:

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

这导致我的库文件被位于/usr/lib/libboost*
但是,我的链接器正在寻找/usr/lib/x86_64-linux-gnu/libboost*

旧文件的简单cp -Pf解决了我的问题。

1

我遇到了非常相似的一段代码完全相同的问题,同时使用程序选项库(版本1.58在我的情况)。

我的解决方案是简单地重新安装升压(相同版本),并且问题解决了没有任何其他代码修改或系统更改。

总之,这个问题似乎并不直接与Boost库相关,但似乎是由于系统的Boost安装。另一个SO question指出了一个类似的问题,根据评论,干净地重新安装相同版本的Boost(他们的案例为1.60)也是成功的。

希望这可以帮助别人!

-1

我有这样的问题太多,我终于找到我的问题的根本原因,也许它会帮助你,

的gdb的核心文件时

,它显示了这样

#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 

我发现它当我编译exe文件时,使用系统包含文件,但它会链接与系统boost不同的boost.a文件。它很惊讶。当我删除系统提升,一切都好!