I'm working on a Particle Photon Internet Button project, and I can't seem to get my code to compile. I have the following code that's more or less what I want which does compille...
static void hello_world(){ Serial.println("Hello, World!"); }
typedef struct DFA_Node DFA_Node_t;
typedef struct DFA_Node{
bool accepting;
void (*handler)(void);
const DFA_Node_t *(*next_node)(char next);
} DFA_Node_t;
#define ESC (0x1B)
#define ACCEPTING_UNIMPLEMENTED_DFA {.accepting = true, .handler = NULL, .next_node = NULL}
extern const DFA_Node_t e;
extern const DFA_Node_t eb;
extern const DFA_Node_t ebA;
const DFA_Node_t *e_f (char next) {return (next == '[' ? &eb : (next == ESC ? &e : NULL));}
const DFA_Node_t *eb_f(char next) {return (next == 'A' ? &ebA : (next == ESC ? &e : NULL));}
const DFA_Node_t e = {.accepting = false, .handler = NULL, .next_node = &e_f};
const DFA_Node_t eb = {.accepting = false, .handler = NULL, .next_node = &eb_f};
const DFA_Node_t ebA = {.accepting = true, .handler = &hello_world, .next_node = NULL};
... but I want all of these to be static as in the following...
static void hello_world(){ Serial.println("Hello, World!"); }
typedef struct DFA_Node DFA_Node_t;
typedef struct DFA_Node{
bool accepting;
void (*handler)(void);
const DFA_Node_t *(*next_node)(char next);
} DFA_Node_t;
#define ESC (0x1B)
#define ACCEPTING_UNIMPLEMENTED_DFA {.accepting = true, .handler = NULL, .next_node = NULL}
static const DFA_Node_t e;
static const DFA_Node_t eb;
static const DFA_Node_t ebA;
static const DFA_Node_t *e_f (char next) {return (next == '[' ? &eb : (next == ESC ? &e : NULL));}
static const DFA_Node_t *eb_f(char next) {return (next == 'A' ? &ebA : (next == ESC ? &e : NULL));}
static const DFA_Node_t e = {.accepting = false, .handler = NULL, .next_node = &e_f};
static const DFA_Node_t eb = {.accepting = false, .handler = NULL, .next_node = &eb_f};
static const DFA_Node_t ebA = {.accepting = true, .handler = &hello_world, .next_node = NULL};
.... but I get the following errors:
uninitialized const 'e'
uninitialized const 'eb'
uninitialized const 'ebA'
redefinition of 'const DFA_Node_t e'
redefinition of 'const DFA_Node_t eb'
redefinition of 'const DFA_Node_t ebA'
I know I had done something similar in the past so I tried compiling locally... I realized locally I'm compiling with gcc and from the Particle Desktop IDE, it's compiling as C++. I tried compiling with g++ locally and got similar errors.... So this seems to just be a C vs C++ question? I'm a little more used to C at this point, so maybe there's a more straight forward way to do this with C++. After some googling around I got rid of the first 3 errors by adding the following struct constructors:
DFA_Node():accepting(false), handler(NULL), next_node(NULL){};
DFA_Node(bool accepting, void(*handler)(void), const DFA_Node_t *(*next_node)(char next)): accepting(accepting), handler(handler), next_node(next_node){};
But I'm still getting the redefinition errors.
Any help would be appreciated.
Thanks!
Nathan