Using a library's function in a separate .ino file

Hi Everyone,

I'm trying to figure out how to do something and I can't seem to find an answer searching, or maybe I'm just not searching for the right thing.

To start with, I'm trying to use the FirebaseJson library.

I have the main .ino file, then I have separate files for different functions. One of them is for configuration functions, let's call it config.ino.

So, in my main .ino file I have the following at the top of the sketch, before the setup or loop functions.

FirebaseJson ConfigJSON;
FirebaseJsonData result;

Then, in the config.ino file I have this.

void CreateConfig() {
	File file = SPIFFS.open(ConfigFile, "w");
	if (!file) {
		Serial.println(F("Failed to create file"));
		return;
	}
	ConfigJSON.setJsonData(
			"<JSON DATA>");
	if (ConfigJSON.toString(file, false)) {
		file.close();
		Serial.println("Created default board config file.");
		SetConfig();
	} else {
		file.close();
		Serial.println("Failed to create default board config file.");
	}
}

When I compile it, I get "error: 'ConfigJSON' was not declared in this scope"

My goal was to be able to use the ConfigJSON anywhere in the sketch, in any file. Is there any easy way to do this?

Did you #include it at the top of each source file, or extern the required variables if they’re already declared in a specific header as global ?

C extern keyword

I tried including the FirebaseJson.h in the confi.ino file, but I'm not familiar with the how to extern...yet.

That worked perfectly after researching it! Thank you!

So, I just had to add the extern statements in the config.ino file to match what I had in the main sketch.

extern FirebaseJson ConfigJSON;
extern FirebaseJsonData result;

Very odd, that should not be required. When you have multiple .ino files the Arduino IDE simply mashes them all together into one big file before doing it's pre-compile stuff (auto prototype generation, etc) and then compiling. The file order is ... the main .ino (same name as project directory name) followed by all other .ino files in alphabetical order. So, if a global variable is defined at the top of the main .ino, it should be in-scope for the entire mash up.

BTW, using multiple .ino files is an Arduino thing and is actually a poor way of achieving true modularity in your project. To do it right, modules need to be split into individual .h / .cpp files. If you're interested in this, see my Post #5 in this thread for some basic guidelines.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.