Tutorial for creating multi cpp file arduino project

The code above is just for learning, I’m not using that and I’m still on Arduino IDE, just learning to convert the Tabs .ino to .h/.cpp. I just installed the PlatformIO but I haven’t started on it yet

Yes, I forgot to add and the file is like that. Where is the right way to put the global variables and #define ?

Nice, I thought that was overload or increase the size or compilation time of the program.

Can I just divide the code into multiples .cpp, put all the prototypes and extern into one .h, and just include this .h into each .cpp ? All the .cpp in the directory are automatically build in ? I put them in “src” in Arduino and it’s worked.

I mean, do this:

main.ino

#include <Arduino.h>

#include "src/0_globals.h"
#include "src/prototypes.h"

void setup(){
        Serial.begin(115200);
        mRun("Running..");
        mWait("Waiting...");
}

void loop(){}

0_globals.h

#ifndef GLOBALS_H
#define GLOBALS_H

int mGlobal = 10;

#endif

prototypes.h

#ifndef PROTOTYPES_H
#define PROTOTYPES_H

#include <Arduino.h>

extern int mGlobal;

void mRun(String s);
void mWait(String s);

#endif

file1.cpp

#include "prototypes.h"

void mRun(String s){
        Serial.println("File 1: "+s);
        mGlobal = 1;
}

file2.cpp

#include "prototypes.h"

void mWait(String s){
        Serial.println("File 2: "+s);
        mGlobal = 2;
}

Ok, thanks. That’s fine.

No problem. Have you managed to get the standard Arduino Blink sketch working in PlatformIO yet? It’s always good, with a new development system, to at least confirm that everything is working before diving in. Ask me how I know! :frowning_face:

In this case, yoi have one global. It would be acceptable to have extern int mGlobal in the prototypes.h file, and int mGlobal = 10; in the main.ino file. Then every source file that needs a function call or access to the mGlobal variable, could simply #include "prototypes.h".

If, on the other hand you have many globals, then you could just as easily have a globals.h where they are all defined as extern, then in the various source files, where they are first used, define them properly without the extern. The potential problem here is defining it extern because you included the header, and having it defined as non-extern, in the same compilation unit. (Source file). That might cause problems, but as I’ve never come across that problem with my own code, I’m not 100% sure it will be a problem. Must find out!

It won’t make much difference to be honest. The compiler will make a bit of space to hold the details of the functions, initialise it, then throw it away when it finished compiling the file. You really wouldn’t notice much delay or excessive use of RAM.

Yes indeed you can. But remember, you only need the header in any source file that calls a function declared in the header, or, which accesses a global variable declared in the header as extern. If neither of these are true, then the header is not needed by that particular source file.

That looks correct to me, yes. Also, you can rename the various ino files to cpp. As long as Arduino.h is included, it will work fine.

HTH

Cheers,
Norm.

1 Like