0

i'm studying about arduino but i want to test my code by g++ compiler before adjust to arduino.

it is easy way to test if i make two same code but it makes me tiresome.

so i want to do conditional compile.

here is my code

CustomMath.cpp

#include "CustomMath.h" #if defined(_CPP_) #include <cmath> #endif #if defined(_ARDUINO_) #include "math.h" #endif float CustomMath::cSin(int degree) { float value = 2;//check for this code is work right float radian = cDegreeToRadian(degree); #if defined(_CPP_) value = sin(radian); #elif defined(_ARDUINO_) value = sin(radian); #endif return value; } 

and here is main code

#define _CPP_ #include <iostream> #include "CustomMath.h" using namespace std; using namespace CustomMath;//CustomMath.h ic braced by namespace CustomMath int main() { cout << "sin : " << cSin(30) << endl; } 

and output is

sin : 2 <<-- this means conditional compile is not work 

i think _CPP__ is defined after CustomMath compiled. how can i do conditial compile? or is there something wrong?

==========================================================================

i just add ComileCondition.h

#define _CPP_ 

and i just change this part when i adjust on arduino

6
  • Possible duplicate of stackoverflow.com/questions/53288150/… Commented Jun 5, 2020 at 2:55
  • 1
    You've defined _CPP_ for main.cpp, but neither _CPP_ nor _ARDUINO_ is defined when compiling CustomMath.cpp. Commented Jun 5, 2020 at 3:07
  • 1
    Be aware that identifiers that begin with an underscore followed by an uppercase letter are reserved. Commented Jun 5, 2020 at 3:08
  • 1
    Perhaps you should instead check for #ifdef _cplusplus (note the double under-score) Commented Jun 5, 2020 at 4:11
  • normally you would write #include <math.h> since it is a standard header. Commented Jun 5, 2020 at 5:10

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.