I routinely compile with #define _CRT_SECURE_NO_WARNINGS.
When I put before the first #include in my main (and only) module, I get a compile-time warning or error. (I don't rememeber which.)
But it works fine if I put that directive after the #pragma once directive in the stdafx.h file.
The two approaches should be identical insofar as the order in which the compiler sees the lines of code.
Must I real put #define _CRT_SECURE_NO_WARNINGS into stdafx.h (or the first #include'd file, which is stdafx.h for me)?
Or am I doing something inexplicably incorrect when I put #define _CRT_SECURE_NO_WARNINGS into my main (and only) module?
If you just want to define _CRT_SECURE_NO_WARNINGS, then place the definition in the "C++/Preprocessor" constants in your project settings. Then you don't need to mess around with where to place the #define in your source code.
But my question was: what difference does it make if I put the #define before #include "stdafx.h" instead of in #include "stdafx.h" itself?
The preprocessor should see the same "stream" of text, except for the interstitial ``#pragma once`` in the latter approach.
"stdafx.h" is the usual name of the wizard-generated precompiled header which has a special meaning for the compiler. Take a look here and here.
and here is the relevant quote for your use case from msdn:
Source File Consistency
When you specify the Use Precompiled Header File (/Yu) option, the compiler ignores all preprocessor directives (including pragmas) that appear in the source code that will be precompiled. The compilation specified by such preprocessor directives must be the same as the compilation used for the Create Precompiled Header File (/Yc) option.
IMHO, if you don't know (or don't care to know) what a precompiled header is and how it works then you should not use it, as it changes the standard compilation model. You can disable it via the project settings.
Using precompiled headers is just a Visual Studio technique to shorten subsequent compile times.
For big projects it may be useful, for smaller ones it doesn't hurt.
It is not obviously necessary to be a VS-guru for using precompiled headers.
Generally, just have to keep in mind two simple things:
Include "StdAfx.h" before any other preprocessor directive, in each implementation source file (.c of .cpp);
The comments written by wizard in the top of StdAfx.h.
Code:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently,
// but are changed infrequently
Last edited by ovidiucucu; December 23rd, 2012 at 06:53 AM.
...all above being said, just add _CRT_SECURE_NO_WARNINGS to "Preprocessor Definitions" in the project settings/properties, as Paul McKenzie already suggested and avoid further headaches.
Last edited by ovidiucucu; December 23rd, 2012 at 07:13 AM.
"stdafx.h" is the usual name of the wizard-generated precompiled header which has a special meaning for the compiler. Take a look here and here.
and here is the relevant quote for your use case from msdn:
[...missing in quoted text provided by the GUI(!)...]
IMHO, if you don't know (or don't care to know) what a precompiled header is and how it works then you should not use it, as it changes the standard compilation model. You can disable it via the project settings.
Thanks for the pointers and explanation. Actually, the quote that does it for me, albeit non-authoritative, is from the wiki page, to wit:
``Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled.``
Your suggestion not to use precompiled headers seems appropriate for my purposes (usually small projects). I just wish it were the default. I use Visual C++ so rarely that I am not likely to remember this and other special option choices that are prudent for me to make for my purposes.
Thanks again.
Last edited by joeu2004; December 23rd, 2012 at 01:32 PM.
Bookmarks