【问题标题】:What is the Preferred Cross-platform 'main' Definition Using boost::program_options?使用 boost::program_options 的首选跨平台“主要”定义是什么?
【发布时间】:2009-06-30 21:20:39
【问题描述】:

我正在尝试使用带有 boost 的 C++ 开发跨平台应用程序。

我通常在 *nix 环境中编程,我总是将 'main' 定义如下:

int main( const int argc, const char* argv[] )
{
...
}

对于这个应用程序,我从 Windows 环境开始,使用 Visual Studio 2003。

当我尝试在此定义中使用 boost::program_options 时,我从 program_options::store: 得到编译错误:

po::options_description desc("Supported options");
desc.add_options()...;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);

错误:

main.cpp(46) : error C2665: 'boost::program_options::store' : none of the 2 overloads can convert parameter 1 from type 'boost::program_options::basic_parsed_options<charT>'
    with
    [
        charT=const char
    ]
    c:\boost_1_38_0\boost\program_options\variables_map.hpp(34): could be 'void boost::program_options::store(const boost::program_options::basic_parsed_options<charT> &,boost::program_options::variables_map &,bool)'
    with
    [
        charT=char
    ]
    c:\boost_1_38_0\boost\program_options\variables_map.hpp(43): or       'void boost::program_options::store(const boost::program_options::basic_parsed_options<wchar_t> &,boost::program_options::variables_map &)'
    while trying to match the argument list '(boost::program_options::basic_parsed_options<charT>, boost::program_options::variables_map)'
    with
    [
        charT=const char
    ]

我试图通过如下定义 main 来强制 wchar_t 函数:

int main( const int argc, wchar_t* argv[]){
...
}

然后它编译,但我得到链接错误:

main.obj : error LNK2019: unresolved external symbol "void __cdecl boost::program_options::store(class boost::program_options::basic_parsed_options<unsigned short> const &,class boost::program_options::variables_map &)"  referenced in function _main
main.obj : error LNK2019: unresolved external symbol "public: __thiscall boost::program_options::basic_parsed_options<unsigned short>::basic_parsed_options<unsigned short>(class boost::program_options::basic_parsed_options<char> const &)"  referenced in function "public: class boost::program_options::basic_parsed_options<unsigned short> __thiscall boost::program_options::basic_command_line_parser<unsigned short>::run(void)" 
main.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl boost::program_options::to_internal(class std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> > const &)"  referenced in function "class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > __cdecl boost::program_options::to_internal<class std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> > >(class std::vector<class std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> >,class std::allocator<class std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> > > > const &)" 

最后,如果我回到 Visual Studio 的默认主定义设置,它会编译并链接:

int main( const int argc, _TCHAR* argv[]){
...
}

所以,这对 Windows 来说很好,但是当我尝试将它带到 *nix 时,它会起作用吗?这些系统通常定义 _TCHAR 类型吗?我个人没有遇到过。

定义 main 以在 Windows 和 *nix 上工作以及使用 boost::program_options 库的正确方法是什么?

【问题讨论】:

    标签: c++ visual-studio portability boost-program-options


    【解决方案1】:

    这似乎是一个与 constness 相关的问题。试试:

    int main( int argc, char* argv[] )
    {
      // ...
    }
    

    【讨论】:

    • 谢谢,成功了。在这个注释上——有人可以向我解释为什么 argc 和 argv 在 main 中不应该是 const 吗?
    • 因为这是 C++ 标准所说的。请注意,所有库示例也都使用正确的签名。
    • 我想我应该问“为什么标准不将它们定义为 const?”。在我看来,这些事情不应该在“主要”的背景下改变。
    • 更改argv[0] 会改变您的程序在ps 的输出中的显示方式
    【解决方案2】:

    int main()int main(int argc, char* argv[])(又名int main(int argc, char** argv))是 C++ 标准认可的签名。

    VisualStudio 尝试以许多疯狂的方式提供帮助,包括尝试确定 (1) 您是否需要 main()WinMain() 以及 (2) 确定您是否需要 chars 或 wchar_t s。如果 VisualStudio 认为您不是在控制台应用程序中工作,您可能需要call split_winmain()

    如果你想强制chars(我会推荐)你可能需要#undef UNICODE

    【讨论】:

      猜你喜欢
      • 2012-05-21
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 2010-09-28
      • 1970-01-01
      • 2014-11-30
      • 1970-01-01
      • 2011-03-11
      相关资源
      最近更新 更多