Why does such a name give VS, an execution function, in a console application?
1 answer
It's simple.
For non-Unicode programs, the standard name main
and the argument list int argc, char* argv[]
.
For Unicode programs, the list of parameters in MSVC looks like int argc, wchar_t* argv[]
, and to avoid conflicts with source codes and compilers that meet the standard, the function is called wmain
.
For programs that can be compiled both in Unicode- and non-Unicode-mode, the argument list looks like int argc, _TCHAR* argv[]
, where the _TCHAR
macro _TCHAR
defined as char
or wchar_t
depending on the value of the _UNICODE
macro. Thus, you need a third name for the function. So it was coined the name _tmain
.
All this, of course, requires some linker magic, which should be able to determine the entry point not only in the main
function, as in the standard, but also in the wmain
/ _tmain
.
If you are sure that you do not need a Unicode application, you can rename the function to main
and give it a list of arguments int argc, char* argv[]
to conform to the standard. Or if you firmly decided that your program will run only under the WinNT / XP / 7 line (and not Win95 / 98), you can stop at the Unicode version and rename the function to wmain
(respectively, the arguments should be int argc, wchar_t* argv[]
).
Late clarification (thanks @alexolut): According to the documentation , _tmain
is defined in <tchar.h>
using typedef
as main
or wmain
, so your program will still have either main
or wmain
.
(And yes, you can omit the parameters, or add the third parameter envp
, or declare the return type void
.)
- Here's another discussion about where the other
WinMain
/wWinMain
alternativewWinMain
: blogs.msdn.com/b/oldnewthing/archive/2007/12/03/6644060.aspx - VladD - great article! Thank you - gecube
- @gecube: Please! I generally breathe unevenly to everything the old new thing. - VladD pm
- Arguments are still optional. It is also curious that with the simultaneous presence of
main
/wmain
the entry point will bemain
. And for the_tmain
option to_tmain
you need to somehow connect<tchar.h>
. Because_tmain
is just#define
, which takes place inmain
orwmain
. - αλεχολυτ - @alexolut: Regarding
#define
added the answer, thanks! Regarding the choice betweenmain
/wmain
, since the linker supports a non-standard entry point, he will have to make a choice. In the presence ofmain
, of course, it must comply with the standard,wmain
theoretically conformal to the standard, the program has the right to containwmain
. - VladD