I am creating a software for Raspberry pi using WiringPi. The problem is that WiringPi will fail if it does not detect a Raspberry Pi. So if I want to do some unit testing without using an actual Raspberry pi I have to check for a constant and do not perform some function calls if I'm in testing.
I have a testing cpp file, where I have my main() function and in the top of the file I have the #define OS_TESTING. I have the classes split in header and cpp files, so in that file I include the needed header files.
The thing is that I have the cpp file named GPS.cpp and here I have the code for GPS.h. In GPS.cpp I do a #ifndef OS_TESTING, but it does not detect it has already been defined in testing.cpp.
My compiling command is as it follows:
g++ testing.cpp GPS.cpp
Is it possible that it does not get defined because I have the #define in a file not included in GPS.cpp? if that is the case, what can I do to fix it?
Is it possible that it does not get defined because I have the #define in a file not included in GPS.cpp?
Yes defines in the source code only have file scope.
if that is the case, what can I do to fix it?
Use a shared header, something like this:
#if !defined(MY_HEADER_H)
#define MY_HEADER_H
#define OS_TESTING
#endif
And then include the header in both in testing.cpp
and GPS.cpp
.
Or define OS_TESTING
via the command line like this: g++ testing.cpp GPS.cpp -DOS_TESTING
.