how to use if else macro while compilation

D

devphylosoff

I have cpp file with :

#if 1
#include"myfile.h"
#else
#include "libfile.h"
#endif

how it works ?
 
V

Victor Bazarov

devphylosoff said:
I have cpp file with :

#if 1
#include"myfile.h"
#else
#include "libfile.h"
#endif

how it works ?

The preprocessor lets through everything between '#if 1' and '#else'
and drops everything between '#else' and '#endif'. The result of
the filtering is

#include"myfile.h"

which then preprocessed, and then the code from 'myfile.h' is itself
included and preprocessed.

V
 
N

Neelesh Bodas

I have cpp file with :

#if 1
#include"myfile.h"
#else
#include "libfile.h"
#endif

how it works ?

w.r.t. subject line: "macros" are considered in the pre-processing
stage, which comes before compilation.

The preprocessor evaluates the #if macro and expands the appropriate
clause(s).
In the above example, since 1 evaluates to true, the file "myfile.h"
will get included.

-N
 
D

devphylosoff

rights, #if 1 is always true, so if I need switch I should use
different condition.

for example
#if sth
....

and compile with -Dsth.
it's really no way to make #if 1 equals false using -D ?
 
M

Michal Nazarewicz

devphylosoff said:
I have cpp file with :

#if 1
#include"myfile.h"
#else
#include "libfile.h"
#endif

how it works ?

Any textbook on C or C++ should explain that. If condition given in #if
statement is true the portion between #if and #else will be compiled
otherwise the part between #else and #endif. (There is also #ifdef,
#ifndef, #elsif.)
 
C

Chris Thomasson

[...]
Any textbook on C or C++ should explain that. If condition given in #if
statement is true the portion between #if and #else will be compiled
otherwise the part between #else and #endif. (There is also #ifdef,
#ifndef, #elsif.)
[...]

#elif
 
G

Guest

rights, #if 1 is always true, so if I need switch I should use
different condition.

for example
#if sth
...

and compile with -Dsth.
it's really no way to make #if 1 equals false using -D ?

Not sure, but perhaps if you define 1 to be 0. I'm not sure of the order
of expansion though.
 
V

Victor Bazarov

Compile with -Dsth=1. Just defining it is like defining it 0.
Unless you're using

#ifdef sth

or

#if defined( sth )
Not sure, but perhaps if you define 1 to be 0. I'm not sure of the
order of expansion though.

:) I believe it's better than #define black white Not possible,
however, macros are named preprocessor values, and '1' is not a name.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,649
Latest member
MargaretCo

Latest Threads

Top