0

I've a little problem, I'm trying to create a library with processor variables for tune it.

So in .ino I declared my preprocessor var, but preprocessor variables appear not defined in my library. (Notice that I've create a folder test/test.h in libraries directory of Arduino IDE)

Some code to reproduce the problem :

test.ino :

#define TEST

#include "test.h"

void setup() {}
void loop() {}

test.h (edited, reverse error message, this is solution)

#if defined(TEST)
  #error "DEFINED"
#else
  #error "NOT DEFINED"
#endif

If I compile it, I fall on #error "NOT DEFINED".

Somebody with an idea ?

Have a good day

  • What you are doing should work IMO, have you tried #define TEST (1) ? – Code Gorilla Aug 4 '17 at 9:09
  • Note that using #defines in your sketch will have no effect at all on any usage of those defines in source code (.c and .cpp files), or the header file included in said source code files, in your library. It will only affect the single instance of the header file that is included in your sketch. Using #define in your sketch to configure a library is seldom does what you actually intend it to when you set out. – Majenko Aug 4 '17 at 11:06
  • You should understand the concept of the Translation Unit to get a better insight into why #define in the sketch won't affect your library's source code operation. – Majenko Aug 4 '17 at 11:13
0

The code is working properly. You have mixed up your error messages. The code should be:

#if defined(TEST)
  #error "DEFINED"
#else
  #error "NOT DEFINED"
#endif

UPDATE: The question was edited after the time of this answer to correct the mixed up error messages.

0

Please, start by reading the Writing a Library for Arduino Tutorial. That will solve your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.