我从一本书(Boost C ++应用程序开发手册)中得到了这个代码,我尝试运行它,用g ++编译但是当我尝试运行它时,它给了我这个错误“在抛出'boost的实例后终止调用: :exception_detail :: clone_impl>' what():未知选项范围 流产(核心倾销)“ 它没有用clang编译, 我编译了“g ++ ex1.cxx -lboost_program_options”和“clang ex1.cxx -lboost_program_options”
#include <boost/program_options.hpp>
#include <boost/program_options/errors.hpp>
#include <iostream>
namespace opt = boost::program_options;
int main(int argc, char* argv[]){
opt::options_description desc("All options");
// 'a' and 'o' are short option names for apples
desc.add_options()("apple,a", opt::value<int>()->default_value(10),
"apples that you have")
("oranges,o", opt::value<int>(), "oranges that you have")("name",
opt::value<std::string>(), "your name")("help", "produce help message");
opt::variables_map vm;
opt::store(opt::parse_command_line(argc, argv, desc), vm);
opt::notify(vm);
if(vm.count("help")){
std::cout << desc << "\n";
return 1;
}
try{
opt::store(opt::parse_config_file<char>("apples_oranges.cfg", desc), vm);
}
catch(const opt::reading_file& e){
std::cout << "Failed to open 'apples_oranges.cfg': " << e.what();
}
opt::notify(vm);
if(vm.count("name")){
std::cout << "Hi, "<< vm["name"].as<std::string>() << "!\n";
}
std::cout << "Fruits count: "
<< vm["apples"].as<int>() + vm["oranges"].as<int>() << std::endl;
return 0;
}
答案 0 :(得分:0)
这里有一个错字
vm["apples"].as<int>();
应该是
vm["apple"].as<int>();
#include <boost/program_options.hpp>
#include <boost/program_options/errors.hpp>
#include <iostream>
namespace opt = boost::program_options;
int main(int argc, char* argv[]){
opt::options_description desc("All options");
// 'a' and 'o' are short option names for apples
desc.add_options()
("apple,a", opt ::value<int>()->default_value(10), "apples that you have")
("oranges,o", opt ::value<int>(), "oranges that you have")
("name", opt ::value<std::string>(), "your name")
("help", "produce help message");
opt::variables_map vm;
opt::store(opt::parse_command_line(argc, argv, desc), vm);
opt::notify(vm);
if(vm.count("help")){
std::cout << desc << "\n";
return 1;
}
try{
opt::store(opt::parse_config_file<char>("apples_oranges.cfg", desc), vm);
}
catch(const opt::reading_file& e){
std::cout << "Failed to open 'apples_oranges.cfg': " << e.what();
}
opt::notify(vm);
if(vm.count("name")){
std::cout << "Hi, "<< vm["name"].as<std::string>() << "!\n";
}
int a_ = vm["apple"].as<int>();
int o_ = vm["oranges"].as<int>();
std::cout << "Fruits count: " << a_ + o_ << std::endl;
}