Ifdef Compiler Error
Contents |
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 error directive in c the company Business Learn more about hiring developers or posting ads with us Stack #error c++ Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of #error in c example 4.7 million 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 #error gcc 1 I have 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 31.1k43203377 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
Error Directive Must Use C++ For The Type Iostream
this answer answered Feb 8 '10 at 12:33 Hans Passant 655k819631606 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 21k44075 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 gener
preprocessor. The preprocessor examines the code before actual compilation of code begins and resolves all these directives before any code is actually generated by regular
#warning In C
statements. These preprocessor directives extend only across a single line of code. k&r preferred method As soon as a newline character is found, the preprocessor directive is ends. No semicolon (;) is c++ #warning expected at the end of a preprocessor directive. The only way a preprocessor directive can extend through more than one line is by preceding the newline character at the http://stackoverflow.com/questions/2221517/how-do-i-generate-an-error-or-warning-in-the-c-preprocessor end of the line by a backslash (\). macro definitions (#define, #undef) To define preprocessor macros we can use #define. Its syntax is: #define identifier replacement
When the preprocessor encounters this directive, it replaces any occurrence of identifier in the rest of the code by replacement. This replacement can be an expression, a statement, a block or simply http://www.cplusplus.com/doc/tutorial/preprocessor/ anything. The preprocessor does not understand C++ proper, it simply replaces any occurrence of identifier by replacement. 1
2
3
#define TABLE_SIZE 100 int table1[TABLE_SIZE]; int table2[TABLE_SIZE]; After the preprocessor has replaced TABLE_SIZE, the code becomes equivalent to: 1
2
int table1[100]; int table2[100]; #define can work also with parameters to define function macros: #define getmax(a,b) a>b?a:b This would replace any occurrence of getmax followed by two arguments by the replacement expression, but also replacing each argument by its identifier, exactly as you would expect if it was a function: 1
2
3
4
5
6
7
8
9
10
11
12
13
14
// function macro #include
2
3
4
5
#define TABLE_SIZE 100 int table1[TABLE_SIZE]; #undef TABLE_SIZE #define TABLE_SIZE 200 int table2[TABLE_SIZE]; This would generate the same code as: 1
2
int table1[100]; int table2[200]; Function macro definitions a
message. You would use ‘#error’ inside of https://gcc.gnu.org/onlinedocs/cpp/Diagnostics.html a conditional that detects a combination of https://en.wikipedia.org/wiki/C_preprocessor parameters which you know the program does not properly support. For example, if you know that the program will not run properly on a VAX, you might write #ifdef __vax__ in c #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 ifdef compiler error 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.
article is written like a manual or guidebook. Please help rewrite this article from a descriptive, neutral point of view, and remove advice or instruction. (February 2013) (Learn how and when to remove this template message) This article includes a list of references, but its sources remain unclear because it has insufficient inline citations. Please help to improve this article by introducing more precise citations. (March 2015) (Learn how and when to remove this template message) (Learn how and when to remove this template message) The C preprocessor or cpp is the macro preprocessor for the C and C++ computer programming languages. The preprocessor provides the ability for the inclusion of header files, macro expansions, conditional compilation, and line control. In many C implementations, it is a separate program invoked by the compiler as the first part of translation. The language of preprocessor directives is only weakly related to the grammar of C, and so is sometimes used to process other kinds of text files. Contents 1 Phases 1.1 Including files 1.2 Conditional compilation 1.3 Macro definition and expansion 1.4 Special macros and directives 1.4.1 Token stringification 1.4.2 Token concatenation 1.5 User-defined compilation errors 2 Implementations 2.1 Compiler-specific preprocessor features 3 Other uses 4 See also 5 References 6 External links Phases[edit] Preprocessing is defined by the first four (of eight) phases of translation specified in the C Standard. Trigraph replacement: The preprocessor replaces trigraph sequences with the characters they represent. Line splicing: Physical source lines that are continued with escaped newline sequences are spliced to form logical lines. Tokenization: The preprocessor breaks the result into preprocessing tokens and whitespace. It replaces comments with whitespace. Macro expansion and directive handling: Preprocessing directive lines, including file inclusion and conditional compilation, are executed. The preprocessor simultaneously expands macros and, in the 1999 version of the C standard,[clarification needed] handles _Pragma operators. Including files[edit] One of the most common uses of the preprocessor is to include another file: #include