Am working on a project that uses two serial ports on a Mega. 1) Serial for communication with a PC using pySerialTransfer and 2) Serial3 for printing debug statements (back to PC via a nano gateway).
I had used a #define statement to give "Serial3" a defined name so I could change it to another Serial port easily in that one line while I was developing things.
works fine in the principal .ino file but I can't get the #define to show up correctly in any included files (am using an #ifndef test at the top of the included header to give it a default which should not be used).
The code will probably describe this better, attached below. I am expecting DEBUGSERIAL.println() to be resolved to Serial3.println() in both the ino and the cpp file, however it is being defaulted to Serial.println() in the included file - i.e. the #ifndef is returning true when I am expecting it to be false. I can't figure out why this is.
I'm using Arduino plugin for VSCode and intellisense is picking up my intention correctly (i.e. is detecting the #ifndef should be returning false as it is #defined in the principal .ino file).
Ah, I think I have misunderstood how these #includes work. Is this the same across Arduino and general C++? Or does some Arduino preprocessor go through all the cpp files in the directory first and pull in all their #includes before looking in the .ino?
I was certain that the principal file first calls its own h file (or in this case the start of the .ino) before working through the #includes. Indeed that's why I put the #define DEBUGSERIAL first in the file.
I've very probably misunderstood the few C++ tutorials I've done...
In the arduino IDE, all the .ino files are combined into one .cpp file. The build chain also generates function prototypes and includes them in the .cpp file as well. It is then compiled as a separate "compilation unit." Any other .cpp files (like libraries, etc.) as also compiled separately. The linker then links all those compilation units together to make the final product to download to your board.
source .ino, .c and .cpp files only include the .h files which they are explicitly told to include (plus .h files which the header files themselves include)
it is not recommended to include .c or .cpp source files as you can then end up with multiply defined references at link time
Thanks @horace actually fair point I completely neglected an include guard on the example .h file. When I'm working with on a bigger project with multiple cpp and h files I have found that the include guards work fine to stop what you describe - but let me know if I've misunderstood