Global, public constants [solved]

Hi.

I'm programing a PLC under Arduino. We use several libraries to manipulate the inputs, outputs, PWM, memory, etc.

Now, I have a problem with passing constants to a library at compiling time. For example:

const int _pepe_ = 10;

#include <Salidas.h>

Salidas Sal;

void setup() {}

void loop() {}

Salidas library is:

#ifndef Salidas_H
#define Salidas_H

#include "Arduino.h"

class Salidas
{
  public:
    Salidas();
  private:
    int a[_pepe_];
};

#endif

I have to pass pepe to the library because it have a list whit "pepe" items.

This form is wrong. ¿why? ¿Do I can pass a constant to library to compile it?

#define _pepe_ 3

Doesn't work too.

The constant has to be in your include file (Salidas.h), otherwise Salidas.cpp won't see it.

Here's one possible solution. Write a pepe.h file conaining this:

#ifndef _PEPE_H
#define PEPE 10
#endif

In main sketch
#include "pepe.h"

In salidas.h:
#include "pepe.h"

This way you'll have just one definition of PEPE.

Or, you could pass the value to the class constructor, and allocate the array dynamically, freeing it in the destructor.

So bad. ¿Isn't any way? Because, I use a multiple sketch that compile the same libraries. If I do that, I have to change the "#define" instruction in every library every time that I change the sketch to compile.

"I do that now..."

tuxduino: I'll try this option.

AWOL:

[...] and allocate the array dynamically [...]

I don't have idea that it is posible. I can't find a documentation about that. Do you can give me a little help?

tuxduino:
Here's one possible solution. Write a pepe.h file conaining this:

#ifndef _PEPE_H
#define PEPE 10
#endif

In main sketch
#include "pepe.h"

In salidas.h:
#include "pepe.h"

This way you'll have just one definition of PEPE.

The salidas.h don't see pepe.h ...

Where did you put pepe.h ?

I don't have idea that it is posible. I can't find a documentation about that. Do you can give me a little help?

Surely you are aware of malloc() and free() or the C++ counterparts new and delete.

tuxduino:
Where did you put pepe.h ?

In the library .h and .cpp and in the sketch. The file pepe.h is in a second TAB in the arduino IDE.

I solved it using malloc:

In the .h of the library:

int *a;

And in constructor:

a = (int *) malloc(_pepe_ * sizeof(int));

Thanks to AWOL, PaulS and others posters that help me in this.

leonelpepe:

tuxduino:
Where did you put pepe.h ?

In the library .h and .cpp and in the sketch. The file pepe.h is in a second TAB in the arduino IDE.

Ok, next question: where is salidas.h ? If it's in the same folder as pepe.h and the main sketch, it must see pepe.h. Of course, if it's somewhere in the libraries subfolder of your sketchbook, you need to move pepe.h.

leonelpepe:
I solved it using malloc:

In the .h of the library:

int *a;

And in constructor:

a = (int *) malloc(_pepe_ * sizeof(int));

Thanks to AWOL, PaulS and others posters that help me in this.

You put "a" in the .h file ? :fearful:

Is it declared inside a class ? Or is it supposed to be a "module-global" variable ?

You put "a" in the .h file ?

Why not? It is a private member of the class (see original post)

AWOL:

You put "a" in the .h file ?

Why not? It is a private member of the class (see original post)

Ah, yes, sorry! :cold_sweat:

I solved it using malloc:

With a call to free(a); in the destructor, right?

PaulS:

I solved it using malloc:

With a call to free(a); in the destructor, right?

Yes and no...

This library is declared during load. And is not destructed at any time. But, I should add the free(a), just in case.

Thanks.