Globally bundling libraries

Hey y'all,

I've been working on an Arduino project for a few weeks now and it has grown a lot since then and got a bit messy. Basically, it is organized, every core part of the program has its own class and I'm including stuff where I need.
~90% of these classes aren't actually used as classes but more as a simple group of variables and methods, so I can create one extern instance of said class and use its methods and content everywhere.

Now, what I want to do is stuff all my globally used classes into one struct so I can use everything everywhere with the simple syntax like core.cookieHandler.get(), core.anotherHandler.value.
I've tried bundling the instances of all these global classes in a construct located in core.h and include that in every file but if I include this in multiple subclasses that are part of this construct as well, Arduino compiler reports multiple definitions of. (Though I'm using #ifndef/#define/#endif)

Is there a way to get this working?
My approach: core.h

#ifndef __AO_CORE_H__
#define __AO_CORE_H__

#include ...

extern struct programCore {
  ClassOne one;
  ClassTwo two;
}core = { CLASSONE, CLASSTWO };


#endif

project.ino

#include <Arduino.h>
#include "core.c"

void setup() {
  Serial.begin(19500);
}

void loop() {
  delay(1000);
  Serial.println(core.one.value);
  delay(1000);
  Serial.println(core.two.value);
}

This works so far!!

ClassOne.cpp

#ifndef CLASSONE
#define CLASSONE

// if I include core here, it errors out!

class ClassOne{

  public:
    ClassOne();
    int value;
    
};

extern ClassOne CONEX;

#endif

~cbrx

As its extern, you want to share one definition between many files.

By having the declaration in the header and initializing it (providing definition), you have effectively given each file that uses the header its own definition. #ifdef guards only prevent multiple definitions when one file includes many files that could all include the 'guarded' file.

Separate the declaration from the definition.

Core.h

#ifndef __AO_CORE_H__
#define __AO_CORE_H__
 
extern struct programCore {
  ClassOne one;
  ClassTwo two;
}core;

#endif

Core.cpp

#include "Core.h"

programCore core = { CLASSONE, CLASSTWO };

Hey, thanks for the quick reply, I actually got it working this way!

The only problem I have is, that it seems to use more RAM when I run it in this structure. It's still just the same classes but now organized in a core struct and included in every .cpp file to have access to all the methods. I'm still using the extern definition in the header of the respective classes so I should only have one global instance of said class, so why does it use more memory?

  • cbrx

When it was all in the header it was most probably inlined as every where that uses it can see the end result ( what you defined the members as ), which might not use any RAM at all as the values may be stored in code. Now its in a separate Cpp file the data (value) is unknown to other files and the produced code has to read the memory location of the value ( RAM ).

You can make the class a template and keep it all in the header if you want it the values to be seen and inlined.