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;
}