Error #ifdef
Contents |
message. You would use ‘#error’ inside of error directive in c a conditional that detects a combination of #error c++ parameters which you know the program does not properly support. For #error in c example example, if you know that the program will not run properly on a VAX, you might write #ifdef __vax__
#error Gcc
#error "Won't work on VAXen. See comments at get_last_object." #endif If you have several configuration parameters that must be set up by the installation in a consistent way, you can use conditionals to detect an inconsistency and report error directive must use c++ for the type iostream it with ‘#error’. For example, #if !defined(FOO) && defined(BAR) #error "BAR requires FOO." #endif The directive ‘#warning’ is like ‘#error’, but causes the preprocessor to issue a warning and continue preprocessing. The tokens following ‘#warning’ are used as the warning message. You might use ‘#warning’ in obsolete header files, with a message directing the user to the header file which should be used instead. Neither ‘#error’ nor ‘#warning’ macro-expands its argument. Internal whitespace sequences are each replaced with a single space. The line must consist of complete tokens. It is wisest to make the argument of these directives be a single string constant; this avoids problems with apostrophes and the like.
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the #warning in c company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions
K&r Preferred Method
Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million
C Preprocessor Message
programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How do I generate an error or warning in the C preprocessor? up vote 19 down vote favorite 1 I have https://gcc.gnu.org/onlinedocs/cpp/Diagnostics.html a program that must be compiled only in DEBUG mode. (testing purpose) How can I have the preprocessor prevent compilation in RELEASE mode? c-preprocessor share|improve this question edited Jun 25 at 23:11 phs 7,05722761 asked Feb 8 '10 at 12:29 Eonil 31k43203376 add a comment| 7 Answers 7 active oldest votes up vote 34 down vote accepted Place anywhere: #ifndef DEBUG #error Only Debug builds are supported #endif share|improve this answer answered Feb http://stackoverflow.com/questions/2221517/how-do-i-generate-an-error-or-warning-in-the-c-preprocessor 8 '10 at 12:33 Hans Passant 653k819571602 add a comment| up vote 11 down vote C provide a #error statement, and most compilers add a #warning statement. The gcc documentation recommends to quote the message. share|improve this answer edited Nov 17 '15 at 17:28 answered Feb 8 '10 at 12:37 philant 22.9k94890 1 @Antonio Right, there is no [more] recommendation there. I replaced the link with one to gcc doc. –philant Nov 17 '15 at 17:29 add a comment| up vote 4 down vote You can use a error directive for that. The following code will throw an error at compile time if DEBUG is not defined: #ifndef DEBUG #error This is an error message #endif share|improve this answer edited Feb 8 '10 at 23:32 answered Feb 8 '10 at 12:34 Laurent Etiemble 20.9k44075 Sorry, I mix pragma and error while typing. Corrected in answer. –Laurent Etiemble Feb 8 '10 at 23:32 add a comment| up vote 4 down vote Maybe something more sofisticated, but it is only copy&paste of previous solutions. :-) #ifdef DEBUG #pragma message ( "Debug configuration - OK" ) #elif RELEASE #error "Release configuration - WRONG" #else #error "Unknown configuration - DEFINITELY WRONG" #endif P.S. There is also one way how to generate warning. Create unreferenced labe like HereIsMyWarning: and don't
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this http://stackoverflow.com/questions/426736/error-macro-names-must-be-identifiers-using-ifdef-0 site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x http://www.complete-concrete-concise.com/programming/c/preprocessor-%E2%80%93-the-error-directive Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Error: macro in c names must be identifiers using #ifdef 0 up vote 24 down vote favorite 6 I have the source code of an application written in C++ and I just want to comment something using: #ifdef 0 ... #endif And I get this error error: macro names must be identifiers Why is this happening? c++ macros c-preprocessor share|improve this question edited Nov 27 '15 at 11:33 error directive in Brian Tompsett - 汤莱恩 3,093122775 asked Jan 9 '09 at 1:27 Eduardo 4,200164567 I think you misremembered; #ifdef 0 is an error in C as well as C++. –Jonathan Leffler Jan 9 '09 at 2:25 2 You are right I misrembered, It does not work in C, I am not going to delete the question because maybe someone in the future makes the same mistake. –Eduardo Jan 9 '09 at 5:36 @Eduardo Thanks for not deleting the question. –kartik Feb 24 '14 at 8:00 add a comment| 6 Answers 6 active oldest votes up vote 47 down vote accepted The #ifdef directive is used to check if a preprocessor symbol is defined. The standard (C11 6.4.2 Identifiers) mandates that identifiers must not start with a digit: identifier: identifier-nondigit identifier identifier-nondigit identifier digit identifier-nondigit: nondigit universal-character-name other implementation-defined characters> nondigit: one of _ a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q
and often underused preprocessor directive. Behaviour of this preprocessor directive is the same for both C and C++ compilers. Purpose The #error directive terminates compilation and outputs the text following the directive. Format #error text All preprocessor directives begin with the # symbol. It must be the first character on the line or the first character on the line following optional white space. Some early compilers flagged an error if # was not the first character on the line. Spaces or tabs are permitted between the # and error, but not escape characters or other symbols or macros. The preprocessor removes white space and concatenates the # and error together. If anything follows the #error directive (other than white space) then the program is malformed. The following are valid uses: #error some error message text # error some error text to display # /* comments are white space */ error some error message to display The following are invalid uses: // #\ is not a valid preprocessor directive # \t error text to output // #" is not a valid preprocessor directive # "" text to output Use It is used to render a program malformed and output the text following the #error directive. The text may be quoted or unquoted (it doesn't matter). No macro expansion takes place. The language specifications do not say how the text following the #error directive is to be treated. The GCC compiler, replaces all white space characters between tokens with a single white space character. I have no reason to believe other compilers behave differently since white space is not considered significant in the C and C++ languages - it serves only to seperate tokens from one another. There are many times when it is useful to halt compilation: code is incomplete code requires particular library versions code uses compiler dependent features code has specific compiler requirements Incomplete Code When developing code, it is common to create stub functions. For the final release, these stub functions need to be implemented. We can let the compiler help us catch unimplemented functions: int my_function( void ) { #error my_function not implemented return 0; } The above code will fail for every compile. It might be more useful to a