locked
#error Directive RRS feed

  • Question

  • Does the #error directive work in MSVC++?

    I ahve the sample code:

    #include <stdio.h>  
    #if MyVAL != 2 
    #error MyVAL must be defined to 2
    #endif 
    int main() 
       {
       return 0;
       }  
    And get the error:
    fatal error C1189: #error :  Must define one of PLATFORM_WIN, PLATFORM_UNIX, PLATFORM_VXWORKS, or PLATFORM_RTX
    Is there a way to implement this code in Visual C++ 2010. Thank you!

    Wednesday, August 1, 2012 5:10 PM

Answers

  • The #error directive does work.  Here is the documentation for it: http://msdn.microsoft.com/en-us/library/c8tk0xsk(v=vs.100).aspx

    Note that, as the message suggests, it works, but it requires you to set the platform up first.  See: http://msdn.microsoft.com/en-us/library/y0tzt8e0(v=vs.100).aspx 

    In general, this means making sure that you've included windows.h, directly or indirectly, before you use the #error directive.  This will cause PLATFORM_WIN to be defined properly, and the directive to work correctly.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by jsestrad Wednesday, August 1, 2012 8:28 PM
    • Unmarked as answer by jsestrad Wednesday, August 1, 2012 8:28 PM
    • Marked as answer by jsestrad Wednesday, August 1, 2012 8:28 PM
    Wednesday, August 1, 2012 5:15 PM
  • On 8/1/2012 1:10 PM, jsestrad wrote:

    Does the #error directive work in MSVC++?
    I ahve the sample code:

    #include <stdio.h>
    #if MyVAL != 2
    #error MyVAL must be defined to 2
    #endif
    int main()
            {
            return 0;
            }

    And get the error:

    fatal error C1189: #error :  Must define one of PLATFORM_WIN, PLATFORM_UNIX, PLATFORM_VXWORKS, or PLATFORM_RTX

    You are hitting somebody else's #error directive in some header you include, before the compiler gets to yours. Which serves to demonstrate that #error directives do in fact work.

    It seems that whichever third-party library you are using wants you to go to Project | Properties | C/C++ | Preprocessor, and add PLATFORM_WIN to the list of macros in Preprocessor Definitions setting.


    Igor Tandetnik

    • Marked as answer by jsestrad Wednesday, August 1, 2012 8:30 PM
    Wednesday, August 1, 2012 5:54 PM

All replies

  • The #error directive does work.  Here is the documentation for it: http://msdn.microsoft.com/en-us/library/c8tk0xsk(v=vs.100).aspx

    Note that, as the message suggests, it works, but it requires you to set the platform up first.  See: http://msdn.microsoft.com/en-us/library/y0tzt8e0(v=vs.100).aspx 

    In general, this means making sure that you've included windows.h, directly or indirectly, before you use the #error directive.  This will cause PLATFORM_WIN to be defined properly, and the directive to work correctly.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by jsestrad Wednesday, August 1, 2012 8:28 PM
    • Unmarked as answer by jsestrad Wednesday, August 1, 2012 8:28 PM
    • Marked as answer by jsestrad Wednesday, August 1, 2012 8:28 PM
    Wednesday, August 1, 2012 5:15 PM
  • On 8/1/2012 1:10 PM, jsestrad wrote:

    Does the #error directive work in MSVC++?
    I ahve the sample code:

    #include <stdio.h>
    #if MyVAL != 2
    #error MyVAL must be defined to 2
    #endif
    int main()
            {
            return 0;
            }

    And get the error:

    fatal error C1189: #error :  Must define one of PLATFORM_WIN, PLATFORM_UNIX, PLATFORM_VXWORKS, or PLATFORM_RTX

    You are hitting somebody else's #error directive in some header you include, before the compiler gets to yours. Which serves to demonstrate that #error directives do in fact work.

    It seems that whichever third-party library you are using wants you to go to Project | Properties | C/C++ | Preprocessor, and add PLATFORM_WIN to the list of macros in Preprocessor Definitions setting.


    Igor Tandetnik

    • Marked as answer by jsestrad Wednesday, August 1, 2012 8:30 PM
    Wednesday, August 1, 2012 5:54 PM
  • I see what I did. It is pretty much just what you said. i was not completely understanding #error. I thought that since fatal error was thrown, it meant that it was not working, but in this case it means it is working just how I intended it to. I feeling kinda dumb now!

    Thanks!

    Wednesday, August 1, 2012 8:33 PM