1

I have the first line in the file as:

#define DEBUG

In my program I have used this variable as:

    #region Preprocessor directives
    #if DEBUG
    #error DEBUG is defined
    #endif
    #endregion

I get this error upon compilation: #error: 'DEBUG is defined'.

Am I mistaking anything?

3
  • 1
    That is exactly what you defined... Your preprocessor directives can basically be read as "when DEBUG is set then show error 'Debug is defined'".So this behaves as it is supposed to (i.e. showing the error). What are you trying to do, and what is the exact problem? Commented Jul 7, 2016 at 12:35
  • OK. I misinterpreted the usage of #error directive. I thought it is used to raise the defined errors at run-time. But now as I imply from your comment above: #error directive is used to raise compile time error. Is that correct? Commented Jul 7, 2016 at 12:48
  • 1
    Yes exactly, as you can see in the following MSDN link it is intended to let you generate an error from a specific location in your code: https://msdn.microsoft.com/en-us/library/x5hedts0.aspx (I'll post this as an answer as well in a minute) Commented Jul 7, 2016 at 12:50

1 Answer 1

3

As I already pointed out in the comments, that is exactly what the `#error' preprocessor directive is intended to do. The description on MSDN says:

#error lets you generate an error from a specific location in your code.

See here on MSDN: https://msdn.microsoft.com/en-us/library/x5hedts0.aspx

So in your case your directive tells the preprocessor to show the error DEBUG is defined when the DEBUG symbol is set.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.