Go Down

Topic: Constants shared from sketch to library (Read 717 times) previous topic - next topic

estratos

It would be nice if we could use in libraries constants and definitions declared in the sketches. This would let us, among other features:

- Conditionally compile libraries according to user definitions in the sketches.
- Use constant values in libraries that have been defined in the sketches.

Thanks for keeping the good work!!

Daniel.

robtillaart


A #define XYZ before the include and a #ifdef in the library code should work. Quick test:

Code: [Select]

#define XYZ 3

#include <test.h>

void setup()
{
  Serial.begin(115200);
  Serial.print(f());
}

void loop(){}


and a test.h
Code: [Select]
// test.h
#ifdef XYZ
int f()
{
  return XYZ;
}
#else
int f()
{
  return -1;
}
#endif


output is depending on the #define XYZ so it worked (if not defined output = -1 else XYZ)

with #defines you kan define constants of all types - int, float string boolean or even complex macros



Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

estratos

It's odd that you example works but the following doesn't:

test.h (library)
Code: [Select]

#ifdef SKETCHDEF
#define LOCALDEF   SKETCHDEF
#else
#define LOCALDEF   0
#endif


test.cpp (library)
Code: [Select]

#include "test.h"

int test(void)
{
  return LOCALDEF;
};


mysketch.pdw
Code: [Select]


# define SKETCHDEF  3
#include "test.h"

void setup()
{
  Serial.begin(9600);
  Serial.flush();

  int res = test();
  Serial.print("test = ");
  Serial.println(res, DEC);
}


Running "test" from my sketch always returns 0. On the other hand, if I define the body of the test function in test.h instead of test.cpp the above example works. I'm a bit confused.

Thanks again!


mmcp42

I suspect
Code: [Select]

# define

is different to
Code: [Select]

#define

not least because the first doesn't do what you expect!

(I think)
there are only 10 types of people
them that understands binary
and them that doesn't

estratos

Yeah, it was a typo. The original code did contain "#define"

robtillaart

Quote
Running "test" from my sketch always returns 0. On the other hand, if I define the body of the test function in test.h instead of test.cpp the above example works. I'm a bit confused. Thanks again!


The .cpp file is not merged on source level with your sketch, it compiles to an .o file and get linked. It can only relate to things included.

The .h file on the other hand is merged with the source file (to be able to reference the .o file) so #defines will be visible for the preprocessor and will be taken into account when compiling.

AFAIK the solution to your problem is to define a "myconstants.h" file that is included both in your sketch and in the library.

More clear?

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

estratos

Thanks, that's what I guessed, hence my initial post. Anyway, this is just a suggestion for future revisions.

Daniel.

Go Up