代码之家  ›  专栏  ›  技术社区  ›  Adam Holmberg

使用boost::program_选项的首选跨平台“main”定义是什么?

  •  3
  • Adam Holmberg  · 技术社区  · 15 年前

    我尝试开发一个使用Boost的C++跨平台应用程序。

    我通常在*nix环境中编程,在这里我总是定义“main”如下:

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

    对于此应用程序,我将使用Visual Studio 2003在Windows环境中启动。

    当我尝试在这个定义中使用boost::program_选项时,我从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时,这会起作用吗?这些系统通常定义一种类型吗?我没有亲身经历过。

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

    2 回复  |  直到 15 年前
        1
  •  5
  •   e.tadeu    15 年前

    这似乎是一个与警察有关的问题。尝试:

    int main( int argc, char* argv[] )
    {
      // ...
    }
    
        2
  •  1
  •   Max Lybbert    15 年前

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

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

    如果你想强迫 烧焦 你可能需要 #undef UNICODE .