This is what it's said on the documentation:
"Preprocessor directives are lines included in the code of our programs that are not program statements but directives for the preprocessor. These lines are always preceded by a hash sign (#). The preprocessor is executed before the actual compilation of code begins, therefore the preprocessor digests all these directives before any code is generated by the statements."
Ok, I think I got it: You place
#if etc
in your code so as to be compiled better under specific situations.
For example, in this situation it will be used a C compiler and not a C++ one if the __cplusplus variable is false:
#ifdef __cplusplus
// C++ code
#else
// C code
#endif
But, on the other hand, inside some Open Source projects I've seen things like:
#if THEME_IS_BLACK
//1st code
#else
//2nd code
#endif
With the above example I want to point out that if THEME_IS_BLACK has nothing to do with compiling the source code, and it seems like it is designed like this so as to work run-time better with the person who uses the program.
As I see it, if a person compiles this code and then give the executable to another person and the latter one run the program and his theme is black, the 1st code will be executed, else the 2nd - nothing to do with compiling.
Is this totally wrong? I have seen far too many #ifs and #elses in codes but I find them unusable as only directives for the preprocessor and I feel that I miss something o.O
In C and C++, code is run thru a preprocessor first before being submitted to the compiler.
Preprocessor directives are commands to control the preprocessor, the compiler never sees them as they're consumed by the preprocessor.
To properly understand what's going on, you need to look at the preprocessed output. If you're on Unix-like system, you can run the preprocessor with cpp. For example create count.cpp as:
1 2 3 4 5
#ifdef USE_LONG
long count = 0;
#else
int count = 0;
#endif
Then look at the output of: cpp count.cpp
and compare it with the output of: cpp -DUSE_LONG count.cpp
With the Visual C++ compiler, it's the /P switch that tells the compiler to generate preprocessor output. It tends to create large files, so I'd switch it on, compile one file, then switch it back off immediately. (If you forget, you'll have grief the next time you build the whole project!)
Running the compiler against example.cpp will generate example.i in the same folder as the .vcproj.
With Visual Studio, you can use the project properties : "C/C++" -> "Preprocessor" -> "Generate Preprocessed Files"
Or set the /P switch in the "Command Line" edit box (or your makefile).
Yes, when you compile a file, it runs the preprocessor first then invoke the compiler. You can use the preprocessor to tweak the code before the compiler gets to it.
Nothing to do with the environment
Preprocessor directives are often used to tune code for a particular environment, but typically something else reads the environment and defines preprocessor items that are passed to the compiler.