0
#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

I'm still a beginner in C++, but I was curious why do you have to include the stdafx.h library and why is the main function different from other IDEs?

user2023328
  • 125
  • 2
  • 11
  • http://stackoverflow.com/questions/4726155/whats-the-use-for-stdafx-h-in-visual-studio –  Mar 29 '15 at 08:57
  • Stdafx.h is not really a library, visual studio creates that file and You should use it to include whatever Your program may desire. – riodoro1 Mar 29 '15 at 09:00
  • The `_tmain` and other `T` macro based stuff was once in support of generating Windows 9x compatible version of a program. It was obsoleted 15 years ago by a more sane solution. And, say, 10 years ago or so supporting Windows 9x became very much a moot issue. So there is no *technical* reason for having that in new programs. My own opinion is that since the reason is not technical, it's non-technical. – Cheers and hth. - Alf Mar 29 '15 at 10:56

2 Answers2

3

The "stdafx.h" file is a file that's used for precompiled headers. If you want, you can remove it, include all headers directly, and turn off precompiled headers in your project options, and it will still work. It will just be slower.

_tmain and _TCHAR are macros that expand to either main and char, or to wmain and wchar_t, depending on whether Unicode is enabled in your project options. Standard C++ only has a int main(int argc, char *argv[]) declaration, and doesn't support any form that takes wchar_t, so you need implementation-specific extensions to get that to work. If you want, you can write int main(int argc, char *argv[]) if you don't want or don't need Unicode in your command-line arguments. And even with that prototype or with int main(void), you can use GetCommandLineW to get the command-line arguments in wide character form.

  • Re "It will just be slower.", or most likely, it will just be faster. That depends. On a lot of things. – Cheers and hth. - Alf Mar 29 '15 at 10:44
  • Re "ou need implementation-specific extensions", it's worth noting that MinGW-64 g++ supports `wmain`, although not to the point of treating it specially (like varying signature). Also, it's only a few lines of code, not difficult to cook up whenever one needs it. – Cheers and hth. - Alf Mar 29 '15 at 10:46
  • Yes, in theory, if you include headers in your stdafx.h that are so small that the cost of including the precompiled headers is higher than the cost of including the real headers, then using precompiled headers will slow things down. But if it's that small, the cost will hardly be noticeable anyway. –  Mar 29 '15 at 10:48
  • Re "you'll merely have to do any conversion to wchar_t yourself if you need it", no that's not a good idea. For the `char` based arguments are by convention ANSI-encoded, discarding characters not in that set. That's incompatible with the standard's recommendation, but, the standard's wording came later. – Cheers and hth. - Alf Mar 29 '15 at 10:49
  • re sizes, you talking some 300 000 lines of code just for ``. If you're not using it, it's a saving to not including it. Precompiled header ornot. (Sorry for keyboard acting up!) – Cheers and hth. - Alf Mar 29 '15 at 10:50
  • @Cheersandhth.-Alf If you don't use ``, instead of disabling precompiled headers, I'd say take it out of your precompiled headers. –  Mar 29 '15 at 10:54
  • That's not unreasonable, but (1) precompiled headers do not help build times for small beginner's programs, and (2) the Visual C++ variant of precompiled headers make the behavior **non-standard**, causing much trouble for the beginners. Standard-conforming code found on the net or in books just doesn't compile. So I recommend to just turn that off. – Cheers and hth. - Alf Mar 29 '15 at 11:00
  • @Cheersandhth.-Alf To be clear, they generally do reduce build times if used properly (no long unused headers), but only by a small amount that for the first few programs may not even be noticeable. I can certainly understand your point of view that it's then better to leave them out. At the same time, though, if the program is expected to grow, then it can be useful to learn how MSVC deals with it, and even beginner programs can benefit from precompiled headers if they use much of the C++ standard library. So I'm not saying leave them in, I'm not saying leave them out, I'm saying choose. :) –  Mar 29 '15 at 11:14
0

It's very Windows specific and is because the Windows graphical programs have other arguments passed to the main function.

The reason for _tmain instead of the standard main is because of Unicode settings when building a Windows-specific console program. If you create a non Windows specific console program without Unicode support you should get the standard main function.

The "stdafx.h" is to handle precomiled headers. Some of the Windows system header files are very large, and parsing a header file for each source file (or rather translation unit) in the project can add a lot of time when building. By "precompiling" the headers, they are in a partially parsed state that is quicker to read and handle for the compiler.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621