c – 使用boost :: program_options的首选跨平台“主要”定义是什么?
作者:互联网
我正在尝试使用C和boost来开发跨平台应用程序.
我通常在* nix环境中编程,我总是在其中定义’main’,如下所示:
int main( const int argc, const char* argv[] )
{
...
}
对于这个应用程序,我使用Visual Studio 2003开始在Windows环境中.
当我尝试使用带有此定义的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库?
解决方法:
这似乎是一个与常数相关的问题.尝试:
int main( int argc, char* argv[] )
{
// ...
}
标签:c,portability,visual-studio,boost-program-options 来源: https://codeday.me/bug/20190827/1743253.html