Can we use wmain() with Unix compilers or it'll work only on Windows?

后端 未结 4 1344
别跟我提以往
别跟我提以往 2020-12-17 18:24

Can we use the wmain() function with Unix compilers or it\'ll work only on/for Windows?

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-17 18:30

    The only standard forms of main are

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

    What should main() return in C and C++?

    All others are non-standard. If you read the document from MS you'll see that wmain is put in the Microsoft Specific section:

    Microsoft Specific

    If your source files use Unicode wide characters, you can use wmain, which is the wide-character version of main. The declaration syntax for wmain is as follows:

    int wmain( );
    int wmain(int argc, wchar_t *argv[], wchar_t *envp[]);
    

    You can also use _tmain, which is defined in tchar.h. _tmain resolves to main unless _UNICODE is defined. In that case, _tmain resolves to wmain.

    main function and command-line arguments

    Its main purpose is to get Unicode parameters such as file names. However it's quite useless and most people don't actually use it because you can just call GetCommandLineW() and CommandLineToArgvW() to get the same thing without any changes to main's signature

    For portability you can use Boost.Nowide so that everything is in UTF-8. Newer Windows 10 and MSVC's standard library also support setting UTF-8 as the active code page and you can get Unicode args with GetCommandLineA or sometimes with the normal main. See What is the Windows equivalent for en_US.UTF-8 locale?

    See also

    • What is the difference between wmain and main?

提交回复
热议问题