[solved] passing #define and how can it work elsewhere

Hi,

I have read a lot about defines in an .ino and using them within a libary. Using VS2017 w Visual Micro btw. Tried (cutted the non-interesting part):

ino:

#define TEST 1

#include "pass_test.h"

pass_test.h:

#ifndef TEST
#error test not defined!
#endif

Result:
pass_test.h: 14:2: error: #error test not defined!
#error test not defined!

Some say it works if the ifdef is used within the .h, for me (as you can see) it does not.

On the other hand, I´m using the MySensors-Gateway, and their libary. My code in .ino:

#define MY_NODE_ID 95
#include <MySensors.h>

begin of the MySensors.h:

#ifndef MySensors_h
#define MySensors_h

#ifdef __cplusplus
#include <Arduino.h>
#endif

// general macros
#if !defined(_BV)
#define _BV(x) (1<<(x)) //!< _BV
#endif

#include "MyConfig.h"


[...]

and within the MyConfig.h, first use of “MY_NODE_ID” at line 1062.:

[...]

#ifndef MY_NODE_ID
#define MY_NODE_ID (AUTO)
#endif

[...]

I can confirm that I can set the node id and it is used, or if I do not, it auto assignes one. So the question for me is: how the heck does it work?!? MY_NODE_ID is not used within MySensors.h and can be used within the MyConfig.h, coming from my .ino file.
I have btw tried to call a .h from my first .h, and use it there, same as on the MySensors example - no luck.

MySensors Libary on git: GitHub - mysensors/MySensors at master

Your .ino / .h files worked as expect for me. Compiling for an Uno with Arduino 1.8.5.

gfvalvo:
Your .ino / .h files worked as expect for me. Compiling for an Uno with Arduino 1.8.5.

What the .. thank you very much for your feedback. As long as there is only the .h, and the file is not included anywhere else, it works :smiley:
I have before tried to create another .h and .cpp, and just removed the cpp from the project, which also threw me an error - seems the cpp had some influence, and only using a .h is the key.
Awesome, thanks again!

EVERY file that you want to include 'test_pass.h' into MUSTdefine 'TEST', not just the .ino. This works fine:

.ino file:

#define TEST 1
#include "pass_test.h"

void setup() {}

void loop() {}

.cpp file:

#define TEST 1
#include "pass_test.h"

pass_test.h:

#ifndef TEST
#error test not defined!
#endif