There is no way to define something in the sketch and expect it influenced the library
ah - it won't work then.
the compiler is compiling things separately. read about compilation units
there is a not so clean way to do it which basically means injecting the library code inside your .ino.
Everything from your "library" needs to be in just one file, with a .hpp suffix
for example your .ino is
#define VAR_V00 true
#define VAR_X00 true
#define VAR_X01 true
#include <secondary.hpp>
void setup() {
Serial.begin(115200);
printAllVariables();
}
void loop() {}
and in your library folder, you have a subfolder called secondary and inside that folder you have the .hpp file
secondary.hpp
#ifndef _SECONDARY_H_
#define _SECONDARY_H_
void printAllVariables();
#if VAR_V00
int VAR_V00A = 0 ;//device 0: Minute day
bool VAR_V01A = 0 ;//device 0: Status led
#endif
#if VAR_X00
int VAR_X00A = 0 ;//device 1: Time mode
int VAR_X00B = 0 ;//device 1: Time start
int VAR_X00C = 0 ;//device 1: Time end
int VAR_X00D = 0 ;//device 1: Time mode
int VAR_X00E = 0 ;//device 1: Time start
int VAR_X00F = 0 ;//device 1: Time end
#endif
#if VAR_X01
bool VAR_X01A = 0 ;//device 2: RGB state
String VAR_X01B = "000000" ;//device 2: RGB color
#endif
void printAllVariables() {
Serial.println("----------");
#if VAR_V00
Serial.println(VAR_V00A);
#endif
#if VAR_X00
Serial.println(VAR_X00A);
#endif
#if VAR_X01
Serial.println(VAR_X01A);
#endif
Serial.println("----------");
}
#endif
You can't use the #define's in the #include'd file if you put them AFTER the #include. Try:
#define VAR_V00
#define VAR_X00
#define VAR_X01
#include "ESP_VarDef.h"
I suppose you refer to the very first post
That won’t work for the library if there is a .cpp as it is compiled separately.
If everything is in the .h including the code (then it’s better named as .hpp) then indeed that works.
It’s not a great practice to have a dependency outside a library that won’t compile if you don’t include the three #define if you use true or false. the hpp should check if the keywords are defined and if not provide a default value. Alternatively just check with #ifdef and then don’t use true or false as it is ignore
Hello, after quite a few tests, I have more or less managed to get it working, thanks to @johnwasser