Can we use the wmain()
function with Unix compilers or it'll work only on/for Windows?
The only standard signatures for main
are:
int main(void);
int main(int argc, char *argv[]);
However, a freestanding implementation can provide extensions/allow other signatures. But those are not guranteed to be portable. wmain
looks like a Windows/VS thing. There's not much chance this will work on a *nix/GNU GCC.
The wmain
signature exists in Windows to handle wide-character command line arguments. Generally, while Windows applications prefer UTF16, Unix applications prefer UTF8 for Unicode string encoding. UTF8 uses regular char
character strings, so the standard main
signature suffices for Unicode-aware Unix appications.
If you want to make a portable console application that does not require Unicode command line parameters, use main
. If you do need Unicode command line parameters, then you need preprocessor directives that will enable the signature appropriate to each environment.
If you are making a cross-platform GUI application, use a special framework, like Qt.
The only standard forms of main
are
int main(void) { /* ... */ } int main(int argc, char *argv[]) { /* ... */ }
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 tomain
unless_UNICODE
is defined. In that case,_tmain
resolves towmain
.
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
wmain()
is windows-specific. Just like _tmain
, if that matters...
Yes, if by "Unix compilers" you mean you are using compilers in Unix, but not necessarily compiling Unix targets. You can pass -municode
to GCC when cross-compiling to Windows targets to make wmain
and wWinMain
work. But still it can't make these main functions work for *nix targets.
© 2022 - 2024 — McMap. All rights reserved.