Importing multi .ino Arduino project

I had functions spread across a five .ino files.
now I renamed all to .cpp, and otherwise did what Convert Arduino file to C++ manually — PlatformIO latest documentation
suggested.

I still get a lot of errors instead of build

src/config.cpp:17:9: error: 'receivedChars' was not declared in this scope
         receivedChars[ndx] = rc;
         ^~~~~~~~~~~~~
src/I2C.cpp:30:23: error: 'MAKE_REMAINING_CAPACITY' was not declared in this scope

are there some good instuction on how to do the migration properly for multiple files?

I suspect you are suffering from “the Arduino IDE does all this for me in the background, and I don’t know what it does!” :wink:

The IDE takes the top level INO file, the one with the same name as the folder it lives in, and appends all the other INO files, in alphabetical order, onto the end of the top one. Your five INO files end up as one single ino file. Because of this, any variables that you have declared at global scope are available to all the five original files.

When you rename the INO files to CPP, all bets are off as they remain five separate files. Suddenly, you get these scope errors.

The solution is as follows:

  • Copy all your global variables from the INO files, and put them in a single header file. Make them all extern.
  • #include the header file in each of the five CPP files that need to access the global variables.
  • In one of the CPP files, probably the existing one where the globals were defined originally, give them a value where required.

You should now be able to compile without these scope errors. For example:

// Original top level INO file, now CPP
// In this file, declare the globals and initialise them.

const int howMany = 64;
char receivedChars[howMany + 1];
int statusBytes = 0;

...

And this is the header file, call it fred.h:

// The above, copied to a header file.
// Note all are now declared extern.

#ifndef FRED_H
#define FRED_H

extern const int howMany;
extern char receivedChars[];
extern int statusBytes;
...

#endif // FRED_H

Now, in your five CPP files, simply #include "fred.h" and this applies to the top level one as well. It doesn’t matter that the variables are declared extern and are actually declared in the same file.

That’s it.

HTH

Cheers,
Norm.

1 Like

Thank you, yes, I have been using Python and Arduino, and forgot lots of the old-fashion/unnecessary/redundant gymnastics C++ requires :slight_smile:

1 Like