I had a really big .ino
file and I decided to break it down into sub-files.
So I had the .ino
file, the globals.h
file and the functions.h
file.
I moved all the global vars and functions to their respective files. This is what is going on inside:
//.ino
#include "globals.h"
#include "functions.h"
//globals.h
*Has header guard
*All variables here are extern
*Contains function prototypes - the definition is in functions.h
*Contains objects (like softWareSerial)
//functions.h
#include "globals.h"
*contains function definitions
However, my code will not work.
I get a huge list of errors, but the last of which is about SoftwareSerial
(in globals.h
)
'SoftwareSerial' does not name a type; did you mean 'SoftwareSerial_h'?
Isn'e the code structure that I posted previously valid?
EDIT: I ask here and not on StackOverflow for example, because the IDE has symo idiosyncracies, like it first loads the .ino
and then all the other files in an alphabetical manner.
EDIT 2: I followed user's Edgar Bonet
advice (thank you). I reached somewhere, but I get multiple definition
errors now. This is the pseudocode to show to display my code structure:
//string_handling_functions.h
HEADER_GUARD
#include <SoftwareSerial.h> //used later. If this call happened in main .ino (even before the call to this header file, code would not work)
#include <model_definitions.h> //used in the function definition in the .cpp file
byte clear_Buffer(bool, SoftwareSerial*);
//string_handling_functions.cpp
#include <Arduino.h>
#include "string_handling_functions.h"
FUNCTION DEFINITION GOES HERE
//model_definitions.h
HEADER_GUARD
#defines
SoftwareSerial object_definition
Other Objects Definitions (libs called in main .ino file)
const char example_var PROGMEM = {0};
//main_ino.ino
#include "string_handling_functions.h"
I get these type of errors now:
\sketch\string_handling_functions.cpp.o (symbol from plugin): In function `GSM_Module_SW_Serial':
(.text+0x0): multiple definition of `GSM_Module_SW_Serial'
\sketch\main_code.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here
All errors are multiple definitions
. The items that fall victim to this error are objects and arrays.
Some extra questions:
- All the vars in
.h
files have to beextern
? - Can they have a value in the header file? (eg
extern char value = {"0"};
)
EDIT 3:
After compying to all the rules, I had to create many files (.h
and .cpp
) to group everything together.
These are the last errors that I get, and they have to do with SoftwareSerial
:
In file included from \sketch_general_vars.cpp:2:0:
model_definitions.h:498:1: error: 'SoftwareSerial' does not name a type; did you mean 'HardwareSerial'?
SoftwareSerial Ter_1_SS_RXOnly (TER_1_RX_PIN, TER_1_TX_PIN);
^~~~~~~~~~~~~~
HardwareSerial
model_definitions.h:499:1: error: 'SoftwareSerial' does not name a type; did you mean 'HardwareSerial'?
SoftwareSerial Ter_2_SS_RXOnly (TER_2_RX_PIN, TER_2_TX_PIN);
^~~~~~~~~~~~~~
HardwareSerial
model_definitions.h:536:1: error: 'SoftwareSerial' does not name a type; did you mean 'HardwareSerial'?
SoftwareSerial GSM_Module_SW_Serial(GSM_RX_PIN, GSM_TX_PIN);
^~~~~~~~~~~~~~
HardwareSerial
exit status 1
'SoftwareSerial' does not name a type; did you mean 'HardwareSerial'?
When I #include <SoftwareSerial.h>
in that file, then so many errors pop up. Its a cat and mouse game.
First of all, the previous Object calls now get multiple definitios
errors, for example multiple definition of
GSM_Module_SW_Serial'`.
Secondly, I get countless of these messages (not only for READ_GPS
):
ino:1817: undefined reference to
READ_GPS'`
READ_GPS
is defined in globals_non_model.h
, defined in globals_non_model.cpp
, included in string_handling_functions.h
. This is then included in the main ino
(#include "string_handling_functions.h"
)
#include "functions.h"
line. arduino.github.io/arduino-cli/0.33/sketch-build-process/…