0

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?

artless noise
  • 21,212
  • 6
  • 68
  • 105
Razican
  • 697
  • 2
  • 10
  • 16
  • 2
    Define `OS_TESTING` on the compiler's command line, as in `g++ -DOS_TESTING ...` That would apply to all source files being compiled, as if defined first thing in each file. – Igor Tandetnik Feb 01 '15 at 14:50

1 Answers1

2

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.

Marco
  • 7,007
  • 2
  • 19
  • 49