首选跨平台“主要”是什么? 定义使用 boost::program_options?

发布于 2024-07-26 00:50:04 字数 3785 浏览 9 评论 0原文

我正在尝试使用 C++ 和 boost 来开发跨平台应用程序。

我通常在 *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 库的正确方法是什么?

I'm trying to develop a cross-platform application using C++ with boost.

I typically program in a *nix environment, where I've always defined 'main' as follows:

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

For this application, I'm starting in the Windows environment, using Visual Studio 2003.

When I try to use boost::program_options with this definition, I get compile errors from 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);

Error:

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
    ]

I tried to force the wchar_t function by defining main as follows:

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

Then it compiles, but I get link errors:

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 &)" 

Finally, if I fall back to the default main definition setup by Visual Studio, it compiles and links:

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

So, that's good for Windows, but will this work when I try to take it to *nix? Do those systems typically define a _TCHAR type? I haven't come across it personally.

What is the proper way to define main to work on Windows and *nix, plus work with the boost::program_options library?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天荒地未老 2024-08-02 00:50:05

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

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

如果您想强制使用char(我推荐),您可能需要#undef UNICODE

int main() and int main(int argc, char* argv[]) (a.k.a. int main(int argc, char** argv)) are the C++ Standards-approved signatures.

VisualStudio tries to be helpful in a lot of crazy ways, including trying to determine if (1) you want a main() or a WinMain() and (2) deciding if you want chars or wchar_ts. If VisualStudio does not think you're working in a console application, you may need to call split_winmain().

If you want to force chars (which I would recommend) you may need to #undef UNICODE.

青朷 2024-08-02 00:50:04

这似乎是一个常量相关的问题。 尝试:

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

It seems to be a constness related problem. Try:

int main( int argc, char* argv[] )
{
  // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文