What is the Preferred Cross-platform 'main' Definition Using boost::program_options?

boost-program-options, c++, portability, visual-studio

Solution

It seems to be a constness related problem. Try:

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

Problem

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?

Original source