[EDIT] @xismn, you replied while I was typing this up. Yeah, I realized
that and wanted to elaborate, but I was trying to think of examples that didn't
throw more preprocessors onto it as I feared it would add to his confusion and
make it more complicated to explain. Hence the terrible examples below and
explanations.
|
I wanted to build on what I meant by "there are other uses for them".
This code for example is the simplest I could think of.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
using namespace std;
int main()
{
#ifdef DEBUG
cout << "DEBUGGER CODE!!!\n";
#endif
#ifndef _DEMO_
cout << "DEMO code\n";
#endif
return 0;
}
|
Let's say I was building this from command line and I had calls to print out some data while it ran so I could test something. I could pass -D (define) to the command line
g++ -o exeFile sourceFile.cpp -DDEBUG |
and it will make the ifdef true so that any code I have between #ifdef DEBUG #endif will print out, but because I didn't define _DEMO_ my code there will print out. To remove _DEMO_ from running during compilation I would do
g++ -o exeFile sourceFile.cpp -D_DEMO_ |
making _DEMO_ now defined and will skip over it.
The advantages of this is you can put multiple of them in your code.
1 2 3 4 5 6 7
|
#ifdef DEBUG
/* ... */
#endif
/*.... 100 lines of code */
#ifdef DEBUG
/* ... */
#endif
|
I think the more common place to use them is in libraries for like Allegro, SDL, SFML, etc where they have OS specific macros and functions:
1 2 3 4 5 6 7
|
#ifdef WIN32
/* macros and windows pecific functions */
#endif
#ifdef LINUX
/* macros and linux specific functions */
#endif
|
These are just examples to help you get your head around them. If you haven't already you may want to also give this a read:
http://www.cplusplus.com/doc/tutorial/preprocessor/