使用提升program_options失败,并出现奇怪的编译错误

Using boost program_options fails with weird compilation error

本文关键字:错误 编译 program 失败 options      更新时间:2023-10-16

我正在尝试使用 Boost 的program_options库为库构建一个简单的命令行应用程序,但它失败了,出现一个非常奇怪的错误。这是所有有问题的代码(是的,它非常简单(:

#include <iostream>
#include <stdexcept>
#include <string>
#include <boost/program_options.hpp>
#include "NCDocEngineInterop.h"
#include "NCDocumentEngine.h"
#include "NCConfigurationManager.h"
#include "NCAcquisitions.h"
namespace po = boost::program_options;
using std::cout;
using std::cerr;
using std::endl;
using std::domain_error;
using std::wstring;
void fail_if_empty(const po::variables_map& variables, const char* field) {
    if (!(variables.count(field))) {
        cerr << field << " is required, you must provide a value for it" << endl;
        throw domain_error(field);
    }
}
int main(int ac, char* av[])
{
    const char* nsdk_path_option = "nsdk-path"; // NC_CONFIGURATION::NC_NSDK_PATH
    const char* ocr_path_option = "ocr-path"; // NC_CONFIGURATION::NC_OCR_PATH
    const char* parser_path_option = "parser-path-option"; // NC_CONFIGURATION::NC_PARSER_PATH
    const char* pdf_path = "pdf-path"; // NC_CONFIGURATION::NC_PDF_PATH
    const char* output_directory = "output-directory";
    const char* input_file = "input-file";
    po::options_description description("Runs the NDSK engine at the provided files");
    description.add_options()  
        ("help", "produce help message")
        (nsdk_path_option, po::value<wstring>(), "path to the NSDK")
        (ocr_path_option, po::value<wstring>(), "path to the OCR")
        (parser_path_option, po::value<wstring>(), "path to the parser")
        (pdf_path, po::value<wstring>(),"path to the PDF processor")
        (output_directory, po::value<wstring>(), "path to the folder where the output will be stored")
        (input_file, po::value<wstring>(), "path to the input file")
    ;
    po::variables_map vm;
    po::store(po::parse_command_line(ac, av, description), vm);
    po::notify(vm);
    fail_if_empty(vm, nsdk_path_option);
    fail_if_empty(vm, ocr_path_option);
    fail_if_empty(vm, parser_path_option);
    fail_if_empty(vm, pdf_path);
    fail_if_empty(vm, output_directory);
    fail_if_empty(vm, input_file);
    NCConfigurationManager::ncSetConfiguration(
        NC_NSDK_PATH, 
        vm[nsdk_path_option].as<wstring>() );
    NCConfigurationManager::ncSetConfiguration(
        NC_OCR_PATH,
        vm[ocr_path_option].as<wstring>() );
    NCConfigurationManager::ncSetConfiguration(
        NC_PARSER_PATH,
        vm[parser_path_option].as<wstring>() );
    NCConfigurationManager::ncSetConfiguration(
        NC_PDF_PATH,
        vm[pdf_path].as<wstring>() );
    return 0;
}

发生的编译错误是:

错误

错误 C2679:二进制"<<":找不到需要 类型为"const src"的右操作数(或者没有可接受的操作数 转换( c:\work\dev\source\src\framework\thirdparty\boost\boost\lexical_cast.hpp 1281 1 DocumentProcessor

错误 C2228:".fail"的左侧必须具有类/结构/联合 C:\工作\数字BP\项目\发布开发sdk_4_0\dev\源\src\框架\第三方\boost\boost\lexical_cast.hpp 1281 1 文档处理器

您可以在此处查看 lexical_cast.hpp 文件。

而且我不知道这里发生了什么,也不知道为什么这个代码会发生此错误。我正在使用 boost 1.50.0,现在无法升级它。我也在使用Visual Studio 2010来构建它。

下面是"输出"窗口的输出:

1>c:worksrcframeworkthirdpartyboostboostlexical_cast.hpp(1281): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const src' (or there is no acceptable conversion)
1>          c:program files (x86)microsoft visual studio 10.0vcincludeostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<wchar_t,std::char_traits<wchar_t>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludeostream(726): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<wchar_t,std::char_traits<wchar_t>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludeostream(968): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<wchar_t,std::char_traits<wchar_t>,const InputStreamable>(std::basic_ostream<_Elem,_Traits> &&,_Ty)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>,
1>              InputStreamable=src,
1>              _Ty=src
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludeostream(1085): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<wchar_t,std::char_traits<wchar_t>>(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludeostream(186): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,_Traits> &(__cdecl *)(std::basic_ostream<_Elem,_Traits> &))'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludeostream(192): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludeostream(199): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludeostream(206): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::_Bool)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludeostream(226): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(short)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludeostream(260): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned short)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludeostream(280): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(int)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludeostream(305): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned int)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludeostream(325): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludeostream(345): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned long)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludeostream(366): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__int64)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludeostream(386): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned __int64)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludeostream(407): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(float)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludeostream(427): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(double)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludeostream(447): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long double)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludeostream(467): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(const void *)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludeostream(487): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_streambuf<_Elem,_Traits> *)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          while trying to match the argument list '(std::basic_ostringstream<_Elem,_Traits,_Alloc>, const src)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>,
1>              _Alloc=std::allocator<wchar_t>
1>          ]
1>          c:worksrcframeworkthirdpartyboostboostlexical_cast.hpp(1499) : see reference to function template instantiation 'bool boost::detail::lexical_stream_limited_src<CharT,Traits,RequiresStringbuffer>::shl_input_streamable<const InStreamable>(InputStreamable &)' being compiled
1>          with
1>          [
1>              CharT=char_type,
1>              Traits=traits,
1>              RequiresStringbuffer=true,
1>              InStreamable=src,
1>              InputStreamable=src
1>          ]
1>          c:worksrcframeworkthirdpartyboostboostlexical_cast.hpp(1974) : see reference to function template instantiation 'bool boost::detail::lexical_stream_limited_src<CharT,Traits,RequiresStringbuffer>::operator <<<Source>(const InStreamable &)' being compiled
1>          with
1>          [
1>              CharT=char_type,
1>              Traits=traits,
1>              RequiresStringbuffer=true,
1>              Source=src,
1>              InStreamable=src
1>          ]
1>          c:worksrcframeworkthirdpartyboostboostlexical_cast.hpp(1921) : while compiling class template member function 'std::wstring boost::detail::lexical_cast_do_cast<Target,Source>::lexical_cast_impl(const Source &)'
1>          with
1>          [
1>              Target=std::wstring,
1>              Source=src
1>          ]
1>          c:worksrcframeworkthirdpartyboostboostlexical_cast.hpp(2133) : see reference to class template instantiation 'boost::detail::lexical_cast_do_cast<Target,Source>' being compiled
1>          with
1>          [
1>              Target=std::wstring,
1>              Source=src
1>          ]
1>          c:worksrcframeworkthirdpartyboostboostprogram_optionsdetailvalue_semantic.hpp(89) : see reference to function template instantiation 'Target boost::lexical_cast<T,std::basic_string<_Elem,_Traits,_Ax>>(const Source &)' being compiled
1>          with
1>          [
1>              Target=std::wstring,
1>              T=std::wstring,
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>,
1>              Source=std::basic_string<char,std::char_traits<char>,std::allocator<char>>
1>          ]
1>          c:worksrcframeworkthirdpartyboostboostprogram_optionsdetailvalue_semantic.hpp(170) : see reference to function template instantiation 'void boost::program_options::validate<T,char>(boost::any &,const std::vector<_Ty> &,T *,long)' being compiled
1>          with
1>          [
1>              T=std::wstring,
1>              _Ty=std::string
1>          ]
1>          c:worksrcframeworkthirdpartyboostboostprogram_optionsdetailvalue_semantic.hpp(163) : while compiling class template member function 'void boost::program_options::typed_value<T,charT>::xparse(boost::any &,const std::vector<_Ty> &) const'
1>          with
1>          [
1>              T=std::wstring,
1>              charT=char,
1>              _Ty=std::string
1>          ]
1>          c:worksrcsamplescppdocumentprocessordocumentprocessormain.cpp(37) : see reference to class template instantiation 'boost::program_options::typed_value<T,charT>' being compiled
1>          with
1>          [
1>              T=std::wstring,
1>              charT=char
1>          ]
1>c:worksrcframeworkthirdpartyboostboostlexical_cast.hpp(1281): error C2228: left of '.fail' must have class/struct/union
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

问题实际上就在这里:

po::store(po::parse_command_line(ac, av, description), vm);

parse_command_line 实例化了一个basic_command_line_parser<charT>,其中 charT 当然是char,而不是wchar_t,因为avchar** 类型。这意味着options_description中的所有字符串必然是basic_string<char>的,即 std::string而不是std::wstring.

如果您考虑一下,这实际上是有意义的,因为命令行选项总是作为普通的旧"c"字符串到达。即使它们是 mbcs 编码的,您也有责任使用正确的区域设置执行转换。不幸的是,这是必要的,因为 MBCS 的解释是特定于区域设置的。

我看了一下是否可以创建options_description的wchar_t版本(如果您想从wmain()使用它,但事实并非如此。 options_description包含硬编码std::string

多字节字符串引用