#define and how to use them - C ++

In the precompiled header, if I do this:

#define DS_BUILD #define PGE_BUILD #define DEMO 

then in the source i do:

 #if (DS_BUILD && DEMO) ---- code--- #elif (PGE_BUILD && DEMO) --- code--- #else --- code --- #endif 

I get an error message:

error: operator '& &' has no operand

I've never seen this before. I am using Xcode 3.2, GCC 4.2 on OS X 10.6.3

+4
source share
4 answers

You need to add defined , since you want to check what you have defined.

 #if defined (DS_BUILD) && defined (DEMO) ---- code--- #elif defined (PGE_BUILD) && defined (DEMO) --- code--- #else --- code --- #endif 
+13
source

You must first decide how to use conditional compilation macros. There are usually two popular approaches. It either

 #define A #define B #ifdef A ... #endif #if defined(A) && defined(B) ... #endif 

or

 #define A 1 #define B 0 #if A ... #endif #if A && B ... #endif 

those. either simply define a macro and #ifdef it with #ifdef and / or #if defined() or define a macro for a numerical value and parse if with #if .

You mix these two approaches in your code example, which usually doesn't make sense. Decide which approach you want to use and follow.

+4
source

The effect of #define DEMO is that during preprocessing, every occurrence of DEMO is replaced with nothing ( '' ). Same thing with #define PGE_BUILD . So, in the second example that you published, you effectively get #elif ( && ) , which, you will agree, does not make much sense to the compiler :).

+2
source

You need to specify values ​​for DS_BUILD , PGE_BUILD and DEMO , or you need to use ifdef

 #define DS_BUILD 1 #define PGE_BUILD 1 #define DEMO 1 

as stated above will work

+1
source

Source: https://habr.com/ru/post/1306305/


All Articles