【发布时间】:2016-02-13 17:20:38
【问题描述】:
我在 Ubuntu 14.04 上,使用 CMake 和 CLion。我正在尝试使用程序选项,以下代码取自其文档中的示例:
#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)(将unicode设置为true)
【问题讨论】:
-
我使用 g++ 4.9.2 和 Boost 1.55 没有错误。
-
我使用的是 Boost 1.60.0
-
我在 DigitalOcean clean VM 上遇到了同样的问题。所以我想要么是 Boost 有问题,要么是我的编译有问题......但我不知道如何测试这些理论。
-
该错误也出现在 Ubuntu 15.04 3.19.0-51-generic 的 boost 1.59 中
-
看起来有一张为此的 Boost 票:svn.boost.org/trac/boost/ticket/11905。我在 Ubuntu 15.04 上使用 Boost 1.58 遇到了同样的问题。它适用于 Boost 1.57。
标签: c++ boost boost-program-options