My problem is first of all, understanding #ifndef
and #ifdef
. I also want to understand the difference between #if
, #ifndef
, and #ifdef
. I understand that #if
is basically an if statement. For example:
#include<iostream>
#define LINUX_GRAPHICS 011x101
int main(){
long Compare = LINUX_GRAPHICS;
#if Compare == LINUX_GRAPHICS
std::cout << "True" << std::endl;
#endif
}
But the others, although I read about them I can't comprehend. They also seem like very similar terms, but I doubt they work similarly. Help would be greatly appreciated.
#if
doesn't have any notion about Compare
or the value it contains, so it probably doesn't do what you intend.
Remember the preprocessor does plain text replacement.
The statement will expand as seen from #if
as
#if Compare == 011x101
and being expanded as
#if 0 == 011x101
which certainly won't yield true
at the preprocessing stage.
The #ifdef
and #ifndef
directives check if a preprocessor symbol was #define
'd at all, either using that (<--) preprocessor directive, or your compilers preprocessor option (most commonly -D<preprocessor-symbol>
).
These don't care if the preprocessor symbol carries an empty value or something. A simple
#define MY_CONDITION
or
-DMY_CONDITION
is enough to satisfy
#ifdef MY_CONDITION
to expand the text coming afterwards (or hide it with #ifndef
).
The Compare
declaration isn't a preprocessor symbol and can't be used reasonably with #ifdef
or #ifndef
either.