Where to put #define _CRT_SECURE_NO_WARNINGS?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Where to put #define _CRT_SECURE_NO_WARNINGS?

  1. #1
    Join Date
    Dec 2012
    Posts
    13

    Where to put #define _CRT_SECURE_NO_WARNINGS?

    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 really 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?
    Last edited by joeu2004; December 23rd, 2012 at 01:52 AM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,423

    Re: Where to put #define _CRT_SECURE_NO_WARNINGS?

    Quote Originally Posted by joeu2004 View Post
    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.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Dec 2012
    Posts
    13

    Re: Where to put #define _CRT_SECURE_NO_WARNINGS?

    Quote Originally Posted by Paul McKenzie View Post
    If you just want to define _CRT_SECURE_NO_WARNINGS, then place the definition in the "C++/Preprocessor" constants in your project settings.
    Thanks. Probably good advice.

    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.

  4. #4
    Join Date
    Oct 2008
    Posts
    1,088

    Re: Where to put #define _CRT_SECURE_NO_WARNINGS?

    Quote Originally Posted by joeu2004 View Post
    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.

  5. #5
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,021

    Re: Where to put #define _CRT_SECURE_NO_WARNINGS?

    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.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  6. #6
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,021

    Re: Where to put #define _CRT_SECURE_NO_WARNINGS?

    ...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.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  7. #7
    Join Date
    Dec 2012
    Posts
    13

    Re: Where to put #define _CRT_SECURE_NO_WARNINGS?

    Quote Originally Posted by superbonzo View Post
    "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.

  8. #8
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,021

    Re: Where to put #define _CRT_SECURE_NO_WARNINGS?

    Well, that's your choice. However, about _CRT_SECURE_NO_WARNINGS it's not bad to have a look in this article:
    Microsoft Visual Studio: Secure CRT Functions
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center