#include <Arduino.h>
#include <HardwareSerial.h>
class Test
{
public:
void printSerialTestFromInclude(void);
void printSerialTestFromIncludeOk(void)
{
Serial.
#ifdef PRINT_OK
println("arduino ide OK see the directive define from header");
#else
println("arduino ide NO see the directive define from header");
#endif
}
};
my .cpp (conditionnal_includes_print.cpp)
#include <Arduino.h>
#include <HardwareSerial.h>
#include "conditionnal_includes_print.h"
void Test::printSerialTestFromInclude(void)
{
Serial.
#ifdef PRINT_OK
println("arduino ide OK see the directive define from cpp");
#else
println("arduino ide NO see the directive define from cpp");
#endif
}
the log serial is:
arduino ide OK see the directive define from header
arduino ide NO see the directive define from cpp
It's normal that .ino and .h contain #define and not .cpp? If you have many class with the same #define dependency you must copy paste all your #define in each cpp you have?
Even stranger... If a function is declared in the header file, the SAME method of the SAME instance will produce DIFFERENT results depending on what was defined when the declaration was seen.
Called from INO:
Called from Header: MethodDeclaredInHeader(): PRINT_OK is defined.
Called from Header: MethodDeclaredInSource(): PRINT_OK is NOT defined
Called from INO:
Called from Source: MethodDeclaredInHeader(): PRINT_OK is NOT defined.
Called from Source: MethodDeclaredInSource(): PRINT_OK is NOT defined
sketch.ino
#define PRINT_OK
#include "test.h"
Test test;
void setup()
{
Serial.begin(115200);
Serial.println("Called from INO: ");
test.bothFromHeader();
Serial.println("Called from INO: ");
test.bothFromSource();
}
void loop() {}
test.h
#include <Arduino.h>
class Test
{
public:
void MethodDeclaredInSource(void);
void MethodDeclaredInHeader(void)
{
Serial.print("MethodDeclaredInHeader(): ");
#ifdef PRINT_OK
Serial.println("PRINT_OK is defined.");
#else
Serial.println("PRINT_OK is NOT defined.");
#endif
}
void bothFromSource();
void bothFromHeader()
{
Serial.print("Called from Header: ");
MethodDeclaredInHeader();
Serial.print("Called from Header: ");
MethodDeclaredInSource();
}
};
test.cpp
#include <Arduino.h>
#include "test.h"
void Test::MethodDeclaredInSource(void)
{
Serial.print("MethodDeclaredInSource(): ");
#ifdef PRINT_OK
Serial.println("PRINT_OK is defined");
#else
Serial.println("PRINT_OK is NOT defined");
#endif
}
void Test::bothFromSource()
{
Serial.print("Called from Source: ");
MethodDeclaredInHeader();
Serial.print("Called from Source: ");
MethodDeclaredInSource();
}
MENIER:
It's normal that .ino and .h contain #define and not .cpp? If you have many class with the same #define dependency you must copy paste all your #define in each cpp you have?
Yes, it is normal for a .cpp file to not include #define statements in your .ino.
If you have a bunch of .cpp and .h files that all depend on one #define, put that #define in a .h and include it everywhere it is needed. Then you only have to change that one .h file.