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

5

15

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

Showboat answered 13/3, 2010 at 12:25 Comment(0)
T
14

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.

Thoughtless answered 13/3, 2010 at 12:29 Comment(0)
E
14

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.

Enlightenment answered 13/3, 2010 at 15:46 Comment(0)
L
4

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

Logogram answered 22/9, 2020 at 2:44 Comment(0)
T
3

wmain() is windows-specific. Just like _tmain, if that matters...

Therefor answered 13/3, 2010 at 13:25 Comment(0)
P
0

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.

Plinth answered 10/6, 2023 at 4:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.