General Configuration.h Library

Hello all,

I’m using a new account since my original one has been somehow deleted. I’m assuming because of lack of use, since it’s been a while since I dropped by…

Anyhow, I’'ve done a very simple sketch where I can use an ESP8266 as a modem and I have the Arduino send me a tweet every time my mailbox opens. That way I don’t need to keep an eye on it. Eventually I can add movement sensors for surveillance or even humidity sensors to detect water leaks.

The sketch works as expected and I’m trilled about it. However, I wanted to clean up the code and pack similar things in local libraries, that is stored on the same folder as the .ino file.

Following the example of the Marlin project, the known Arduino Firmware for 3D printers, I wanted to have a Configuration.h file with all the info, such as WiFi Name, Password, tweetThing Key, etc. This Configuration.h file should be accessible for other libraries, such as a “modem library” which will handle the communication with the wifi Module.

However to my dismay, if I try to include the configuration.h file into the header file of another library, the compiler does not allow the code to compile. So I’m guessing I’m missing something here and I don’t know what. I’ve been looking for a solution, but to no avail.

I attach an example of the project, without any substantial code to make things easier…

Any help will be appreciated!

Thanks in advance,

FH

libTest.zip (2.34 KB)

the compiler does not allow the code to compile.

Did the compiler say "Nope, can't let you do that" without commenting what that was or why?

Thank you for replying... you're right, I forgot. Here is the output of my console when the error pops up:

sketch/libTest.ino.cpp.o (symbol from plugin): In function `stringTwo':
(.text+0x0): multiple definition of `stringTwo'
sketch/Library.cpp.o (symbol from plugin):(.text+0x0): first defined here
sketch/libTest.ino.cpp.o (symbol from plugin): In function `stringTwo':
(.text+0x0): multiple definition of `stringOne'
sketch/Library.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino/Genuino Uno.

FelixH:
Here is the output of my console when the error pops up:

google is capable of explaining why you 1) shouldn’t initialize globals in your header and 2) why you should be doing it using the extern keyword like this:

.ino

#include "Configuration.h"

void setup() 
{
  
}

void loop() 
{

}

Configuration.h

#ifndef Configuration_H
#define Configuration_H

#include "Arduino.h"
#include <SoftwareSerial.h>
#include <Bounce.h>


extern String stringOne;
extern String stringTwo;

#define buttonPin 8
#define rxPin 10
#define txPin 11
#define debugBaud 9600

#endif

Configuration.cpp

#include "Arduino.h"
#include "Configuration.h"

String stringOne = "one";
String stringTwo = "two";

BulldogLowell:
google is capable of explaining why you 1) shouldn’t initialize globals in your header and 2) why you should be doing it using the extern keyword like this:

It’s difficult to find the answer when you don’t know what exactly the question is. That’s the purpose of the community forums.

FelixH:
It's difficult to find the answer when you don't know what exactly the question is. That's the purpose of the community forums.

So, now do you see what the problem is/was?

I'm beginning to see it, yes. Still I'm so confused about how Marlin does it. But I guess it's like children being amazed when a grown up steals their nose... it's so far away from their league that looks like wizardry...

Still I'm so confused about how Marlin does it

Does what? I'm sure that Marlin does NOT declare and initialize variables in its header files.

Ever compilation unit that you include the header file in will generate a global variable named stringOne and one called stringTwo. That is NOT what you want.