Home > preprocessor directives > ifndef error

Ifndef 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 the company Business Learn c++ ifdef more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags c++ preprocessor directives Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, ifdef __cplusplus 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 a program that must be preprocessor directives in c 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 this answer answered Feb 8 '10 at 12:33 Hans Passant 655k819631606 add

#define C

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 generate warning. Create unreferenced labe like HereIsMyWarning: and don't reference it. During compilation you will get warning like 1>..\Example.c(71) : warning C4102: 'HereIsMyWarning' : unrefe

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 company Business Learn more

Preprocessor Directives In C++ With Example

about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users ifdef c++ example Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping c++ define macro each other. Join them; it only takes a minute: Sign up Linking error (#ifndef doesn't work as expected) up vote 1 down vote favorite Can't understand what is a problem here: I have got main.cpp file where I http://stackoverflow.com/questions/2221517/how-do-i-generate-an-error-or-warning-in-the-c-preprocessor am including: #include "lexan.h" ... The lexan.h file: #ifndef _LEXAN_ #define _LEXAN_ enum Statements ... //some function prototypes ... struct TokensList { Statements statement; std::string value; struct TokensList *next; }; struct TokensList *tokens = NULL; #endif _LEXAN_ In lexan2.h: #include "lexan.h" // and some function prototypes The problem is that I get the link error 2005: 1>lexan2.obj : error LNK2005: "struct TokensList * tokens" (?tokens@@3PAUTokensList@@A) already defined in lexan.obj 1>main.obj : error LNK2005: "struct TokensList * tokens" http://stackoverflow.com/questions/9835155/linking-error-ifndef-doesnt-work-as-expected (?tokens@@3PAUTokensList@@A) already defined in lexan.obj Where is my mistake? I thought the #ifndef _LEXAN_ #define _LEXAN_ in the lexan.h file would protect me from such linking problems. c++ share|improve this question edited Mar 23 '12 at 6:51 asked Mar 23 '12 at 6:46 maximus 1,41753574 2 Show the code where you are creating struct TokensList * tokens. Most likely you are creating it in the header file lexan.h and then including the header in both lexan.cpp & lexan2.cpp which violates the One Definition Rule(ODR). –Alok Save Mar 23 '12 at 6:47 I have just updated the question you can see now that struct TokensList *tokens is defined right after the struct TokensList definition –maximus Mar 23 '12 at 6:51 add a comment| 3 Answers 3 active oldest votes up vote 3 down vote accepted You are creating struct TokensList * tokens in the header file lexan.h and then including the header in both lexan.cpp & lexan2.cpp which violates the One Definition Rule(ODR). Note that, header guards prevent including the same header in the same Translation Unit. When you create a variable in the header file, a copy of the variable with the same name gets created in every translation unit where you include the header. This leads to multiple same named variables in your project which the linker complains about. Solution: If you need to shar

They must begin on a separate line. Syntax: #if constant_expression #else
#endif

or

#if
https://www-s.acm.illinois.edu/webmonkeys/book/c_guide/1.7.html constant_expression #elif constant_expression #endif The compiler only compiles the code http://www.keil.com/forum/10237 after the #if expression if the constant_expression evaluates to a non-zero value (true). If the value is 0 (false), then the compiler skips the lines until the next #else, #elif, or #endif. If there is a matching #else, and the preprocessor directives constant_expression evaluated to 0 (false), then the lines between the #else and the #endif are compiled. If there is a matching #elif, and the preceding #if evaluated to false, then the constant_expression after that is evaluated and the code between the #elif and the #endif is compiled only if this expression preprocessor directives in evaluates to a non-zero value (true). Examples:

 int main(void) { #if 1 printf("Yabba Dabba Do!\n"); #else printf("Zip-Bang!\n"); #endif return 0; } 
Only "Yabba Dabba Do!" is printed.
 int main(void) { #if 1 printf("Checkpoint1\n"); #elif 1 printf("Checkpoint2\n"); #endif return 0; } 
Only "Checkpoint1" is printed. Note that if the first line is #if 0, then only "Checkpoint2" would be printed.
 #if OS==1 printf("Version 1.0"); #elif OS==2 printf("Version 2.0"); #else printf("Version unknown"); #endif 
Prints according to the setting of OS which is defined with a #define. 1.7.2 #define, #undef, #ifdef, #ifndef The preprocessing directives #define and #undef allow the definition of identifiers which hold a certain value. These identifiers can simply be constants or a macro function. The directives #ifdef and #ifndef allow conditional compiling of certain lines of code based on whether or not an identifier has been defined. Syntax: #define identifier replacement-code #undef iden

30 Days In the Last 90 Days In the Last 6 Months Home/Discussion Forum #ifndef does not work properly Next Thread | Thread List | Previous Thread Start a Thread | Settings Details Message Read-Only AuthorMarkus Lang Posted17-Jul-2007 12:03 GMT ToolsetNone #ifndef does not work properly Markus Lang Hello! I want to include the same headerfile in more than one sourcefile and of course have to take care that each Headerfile is included just once. I really have no clue, why this works for the Headers provided by Keil but not with my own ones: #ifndef _MYHEADER_H_ #define _MYHEADER_H_ //Headerfile content . .. #endif Obviously the preprocessor includes the Headerfile more than once and so i get errors. When i add a statement within my Header like: #warning "included more than once" the warning message appears twice. (so the header is really included twice). Can anyone tell me the problem please?! Kind regards Read-Only AuthorJoost Leeuwesteijn Posted17-Jul-2007 12:16 GMT ToolsetNone RE: #ifndef does not work properly Joost Leeuwesteijn Obviously the preprocessor includes the Headerfile more than once More than once in a single file? I assume you mean in different .c files. Could it be that you see the multiple #warning statements from 2 different .c file compilations in a build environment? Each individual .c file does need to include the .h file when it is compiled. Are you using any #undef's? -- Joost Read-Only AuthorMarkus Lang Posted17-Jul-2007 12:53 GMT ToolsetNone RE: #ifndef does not work properly Markus Lang Thanks for the reply! Yes i mean more than once in different .c Files. There are two .c Files, which both include the same Headerfile. Within this Headerfile i have used the #ifdef .... #endif commands to avoid that the preprocessor includes the Headerfile a second time when it was already included by the first .c File. I donÂ't use #undefÂ's anywhere in code! Thanks for your support ;-) Read-Only AuthorAndy Neil Poste

 

Related content

c preprocessor directives error

C Preprocessor Directives Error table id toc tbody tr td div id toctitle Contents div ul li a href C Preprocessor Directives Examples a li li a href Visual Studio Preprocessor Directives a li li a href warning In C a li li a href error Gcc a li ul td tr tbody table p here for a quick overview of the site Help Center Detailed relatedl answers to any questions you might have Meta p h id C Preprocessor Directives Examples p Discuss the workings and policies of this site About Us Learn preprocessor directives in c programming more