views:

564

answers:

2

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?

+6  A: 

It seems to be a constness related problem. Try:

int main( int argc, char* argv[] )
{
  // ...
}
e.tadeu
Thanks, that worked. On this note -- can somebody explain to me why argc and argv should not be const in main?
Adam
Because that is what C++ standard say. Note that all library examples use the proper signature as well.
Vladimir Prus
I guess I should have asked "why doesn't the standard define them as const?". It seems to me that those things should not change in the context of 'main'.
Adam
+2  A: 

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.

Max Lybbert