I'm trying to use #ifndef as below.
#ifndef MACRO1 || #ifndef MACRO2
....
#endif
I already tried:
#ifndef (MACRO1 || MACRO2)
..
#endif
But for both cases am getting below error
error: extra tokens at end of #ifndef directive
Use the #if preprocessor directive instead:
#if !defined(MACRO1) || !defined(MACRO2)
#ifdef and #ifndef are special abbreviations for #if defined(...) and #if !defined(...). However, they can only be used for a single macro and do not allow for logical operations. So, if checking for multiple macros, use #if with the defined() operator instead. Being a regular operatior, this can be combined with logical operations, as the for !defined() already does.
You can use following code
#if !defined(MACRO1) || !defined(MACRO2)
#endif
You can use the defined operator in the #if directive to use
expressions that evaluate to 0 or 1 within a preprocessor line.
You can use logical operators in preprocessor directives, but in order to check something defined, use the defined directive:
#if !defined MACRO1 || !defined MACRO2
....
#endif
Related
Let us presume that we have the next fragment of code in a C program:
#ifdef USE_FORK
CODE...
#else
phtread_t thread;
pthread_create(&thread,NULL,clientDispatch,&client);
#endif
Can you explain me what are these directives, ifdef, else, endif. What happens when we use C directives?
Quoting cplusplus.com,
Preprocessor directives are lines included in the code of programs preceded by a hash sign (#). These lines are not program statements but directives for the 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 statements.
#ifdef allows a section of a program to be compiled only if the macro that is specified as the parameter has been defined, no matter which its value is. For example:
#ifdef TABLE_SIZE
int table[TABLE_SIZE];
#endif
In this case, the line of code int table[TABLE_SIZE]; is only compiled if TABLE_SIZE was previously defined with #define, independently of its value. If it was not defined, that line will not be included in the program compilation.
The #if, #else and #elif (i.e., "else if") directives serve to specify some condition to be met in order for the portion of code they surround to be compiled. The condition that follows #if or #elif can only evaluate constant expressions, including macro expressions. For example:
#if TABLE_SIZE > 200
#undef TABLE_SIZE
#define TABLE_SIZE 200
#elif TABLE_SIZE < 50
#undef TABLE_SIZE
#define TABLE_SIZE 50
#else
#undef TABLE_SIZE
#define TABLE_SIZE 100
#endif
int table[TABLE_SIZE];
Notice how the entire structure of #if, #elif and #else chained directives ends with #endif.
http://www.acm.uiuc.edu/webmonkeys/book/c_guide/1.7.html
This link should explain to you the directives. Additionally, with the directives if the condition after #ifdef becomes true then the compiler will compile the following code otherwise it would look for the next directive and compile the following code.
In your example therefore, if USE_FORK is a true expression then the CODE... will be compiled, otherwise, the lines with definition to threads will be compiled.
Its a conditional group, a preprocessor command. If the macro is defined i.e. USE_FORK then the code will executed up to #else, if the macro is not defined then the code at #else will execute up to #endif
I have a pre-processor directive as such,
//#define SPEC_CONTROL // Is not defined, but can be if need be
#define SPEC_A_CONTROL // Is defined
#ifdef SPEC_CONTROL || SPEC_A_CONTROL
; // do something
#else
; // do something else
#endif
Is it correct syntax to use the || in the manner I did?
I can assure yout that at least
#if defined(SPEC_CONTROL) || defined(SPEC_A_CONTROL)
works on Windows and several UNIX platforms. Im not sure about
#ifdef SPEC_CONTROL || SPEC_A_CONTROL
You can use boolean logic in a C preprocessor directive by using the if defined directive.
See this post for more information:
C Preprocessor testing definedness of multiple macros
I have a piece of code which I want to include if either of two macros are defined
#ifdef MACRO1 || MACRO2
void foo()
{
}
#endif
How do I accomplish this in C?
Besides #ifdef, the preprocessor supports the more general #if instruction; actually, #ifdef MACRO is a shortcut for #if defined(MACRO), where defined is a "preprocessor function" that returns 1 if the macro is defined; so, you can do:
#if defined(MACRO1) || defined(MACRO2)
void foo()
{
}
#endif
#if defined(MACRO1) || defined(MACRO2)
Here the NOT version if needed:
#if !defined(MACRO1) && !defined(MACRO2)
...
#endif
#if defined(Macro 1) + defined(Macro 2) == 1
<Code>
#endif
What is the difference (if any) between the two following preprocessor control statements.
#if
and
#ifdef
You can demonstrate the difference by doing:
#define FOO 0
#if FOO
// won't compile this
#endif
#ifdef FOO
// will compile this
#endif
#if checks for the value of the symbol, while #ifdef checks the existence of the symbol (regardless of its value).
#ifdef FOO
is a shortcut for:
#if defined(FOO)
#if can also be used for other tests or for more complex preprocessor conditions.
#if defined(FOO) || defined(BAR)
I searched the site but did not find the answer I was looking for so here is a really quick question.
I am trying to do something like that :
#ifdef _WIN32 || _WIN64
#include <conio.h>
#endif
How can I do such a thing? I know that _WIN32 is defined for both 32 and 64 bit windows so I would be okay with either for windows detection. I am more interested in whether I can use logical operators like that with preprocessor directives, and if yes how, since the above does not work.
Compiling with gcc I get :
warning: extra tokens at end of #ifdef directive , and it basically just takes the first MACRO and ignores the rest.
Try:
#if defined(_WIN32) || defined(_WIN64)
// do stuff
#endif
The defined macro tests whether or not a name is defined and lets you apply logical operators to the result.
You must use #if and special operator defined
I think it should be possible this way:
#if defined block1 || defined block2 /*or any other boolean operator*/
/*Code*/
#endif
More information here
Use defined:
#if defined(A) || defined(B)
#include <whatever.h>
#endif