
RP6 ROBOT SYSTEM - 4. Programming the RP6
4.4.11. The C-Preprocessor
In this chapter we will briefly discuss the C-preprocessor, which has been used in the
preceding programming samples already in the line:
#include "RP6RobotBaseLib.h"
!
The preprocessor evaluates this command before starting the GCC-compiling process.
The command line
#include "file"
inserts the contents of the specified file at the in-
clude's position. Our example program includes the file RP6BaseLibrary.h, providing
definitions of all user accessible functions of the RP6Library to allow the compiler to
find these functions and to control the compiling process.
However, the preprocessor features a few other options and allows you to define con-
stants (which may be considered as fixed values to the system):
#define THIS_IS_A_CONSTANT 123
This statement defines the text constant “THIS_IS_A_CONSTANT“ with a value of
“123”. The preprocessor simply replaces all references to it by the defined value. Con-
stants may be considered as text replacements! In the following statement:
writeInteger(
THIS_IS_A_CONSTANT
,DEC);
“THIS_IS_A_CONSTANT” will be replaced with “123” and is identical to:
writeInteger(123,DEC);
(by the way: the parameter “DEC” in writeInteger is just another constant – in this
case defining the constant base value 10 – for the decimal numbering system.)
The preprocessor also knows simple if-conditions:
1
2
3
4
5
6
7
8
9
#define DEBUG
void
someFunction
(
void
)
{
// Now execute something...
#ifdef DEBUG
writeString_P
(
"someFunction has been executed!"
);
#endif
}
This text output will only be performed if “DEBUG” has been defined (you do not have
to assign a value to it – simply defining DEBUG is enough). This is useful to activate
several text outputs for debugging phases during program development, whereas for
normal compiling you can remove these outputs by outcommenting a single line.
Not defining DEBUG in the preceding sample program would prevent the preprocessor
to pass the contents of program line 7 to the compiler.
The RP6Library also provides macros, which are defined by using a #define statement.
Macros allow to process parameters similar to functions. The following example shows
a typical a macro definition:
#define setStopwatch1(VALUE) stopwatches.watch1 = (VALUE)
This definition allows you to call the macro just like a normal function
(e.g.
setStopwatch1(100);
).
An important detail: You usually do not add semicolons after preprocessor definitions!
- 77 -