Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the Preferred Cross-platform 'main' Definition Using boost::program_options?
    primarykey
    data
    text
    <p>I'm trying to develop a cross-platform application using C++ with boost. </p> <p>I typically program in a *nix environment, where I've always defined 'main' as follows:</p> <pre><code>int main( const int argc, const char* argv[] ) { ... } </code></pre> <p>For this application, I'm starting in the Windows environment, using Visual Studio 2003.</p> <p>When I try to use boost::program_options with this definition, I get compile errors from program_options::store:</p> <pre><code>po::options_description desc("Supported options"); desc.add_options()...; po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); </code></pre> <p>Error:</p> <pre><code>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&lt;charT&gt;' 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&lt;charT&gt; &amp;,boost::program_options::variables_map &amp;,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&lt;wchar_t&gt; &amp;,boost::program_options::variables_map &amp;)' while trying to match the argument list '(boost::program_options::basic_parsed_options&lt;charT&gt;, boost::program_options::variables_map)' with [ charT=const char ] </code></pre> <p>I tried to force the wchar_t function by defining main as follows:</p> <pre><code>int main( const int argc, wchar_t* argv[]){ ... } </code></pre> <p>Then it compiles, but I get link errors:</p> <pre><code>main.obj : error LNK2019: unresolved external symbol "void __cdecl boost::program_options::store(class boost::program_options::basic_parsed_options&lt;unsigned short&gt; const &amp;,class boost::program_options::variables_map &amp;)" referenced in function _main main.obj : error LNK2019: unresolved external symbol "public: __thiscall boost::program_options::basic_parsed_options&lt;unsigned short&gt;::basic_parsed_options&lt;unsigned short&gt;(class boost::program_options::basic_parsed_options&lt;char&gt; const &amp;)" referenced in function "public: class boost::program_options::basic_parsed_options&lt;unsigned short&gt; __thiscall boost::program_options::basic_command_line_parser&lt;unsigned short&gt;::run(void)" main.obj : error LNK2019: unresolved external symbol "class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; __cdecl boost::program_options::to_internal(class std::basic_string&lt;unsigned short,struct std::char_traits&lt;unsigned short&gt;,class std::allocator&lt;unsigned short&gt; &gt; const &amp;)" referenced in function "class std::vector&lt;class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt;,class std::allocator&lt;class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; &gt; &gt; __cdecl boost::program_options::to_internal&lt;class std::basic_string&lt;unsigned short,struct std::char_traits&lt;unsigned short&gt;,class std::allocator&lt;unsigned short&gt; &gt; &gt;(class std::vector&lt;class std::basic_string&lt;unsigned short,struct std::char_traits&lt;unsigned short&gt;,class std::allocator&lt;unsigned short&gt; &gt;,class std::allocator&lt;class std::basic_string&lt;unsigned short,struct std::char_traits&lt;unsigned short&gt;,class std::allocator&lt;unsigned short&gt; &gt; &gt; &gt; const &amp;)" </code></pre> <p>Finally, if I fall back to the default main definition setup by Visual Studio, it compiles and links:</p> <pre><code>int main( const int argc, _TCHAR* argv[]){ ... } </code></pre> <p>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.</p> <p>What is the proper way to define main to work on Windows and *nix, plus work with the boost::program_options library?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload