我将尝试使用boost库和C++开发跨平台应用程序。
我通常在*nix环境下编程,其中我一直将'main'定义为以下内容:
我尝试通过以下方式定义main函数来强制使用wchar_t函数:
然后它编译了,但我收到了链接错误:
我通常在*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类型吗? 我个人还没有遇到过它。
如何正确地定义主函数以使其在Windows和*nix上工作,并使用boost::program_options库?
argv[0]
会改变您的程序在ps
输出中的显示方式。 - Gaius