When I try to build this simple project linker trows this nice error
Linking .pio/build/esp32doit-devkit-v1/firmware.elf
.pio/build/esp32doit-devkit-v1/src/main.cpp.o:(.literal._Z4loopv+0x0): undefined reference to `debugSetup()'
.pio/build/esp32doit-devkit-v1/src/main.cpp.o: In function `loop()':
main.cpp:(.text._Z4loopv+0x3): undefined reference to `debugSetup()'
collect2: error: ld returned 1 exit status
*** [.pio/build/esp32doit-devkit-v1/firmware.elf] Error 1
The main reason is “DEBUG_SUPPORT expands in debug.cpp as 0, and same time expands as 1 in main.cpp”. So can someone explain this situation? 10x
When the file debug.cpp is built, it includes debug.h. However, since debug.h does not include config/hw.h, it is unaffected by the definition of DEBUG_SERIAL. Thus it is not set, and thus it doesn’t enable the macro which enables the building of the functinon. Fix it by adding #include <config/hw.h> at the top of debug.h where you use all these defines.
This is some kind of workaround. Actually i need to “fix” build order ( load config with macro, define all DEBUG flags like DEBUG_SUPPORT ) and then build all other things. It is possible, or not?
Would something like this help? It’s a standalone header file I use for some Arduino ESP8266 debugging, and the functions are either present or optimised out if the master DEBUG flag isn’t set. Might give you some ideas anyway!
i.e. at the top of my code I have
#define DEBUG true
#define DEBUG_OI Serial
#include "debug.h"
No it’s not o_O. It’s a plain C/C++ error, not the error of a build system. Create this project in any other IDE for windows or linux and you will get the exact same behavior. You must include the file which defines your needed macros before you use them, since the #define .. done during one compilation unit is not globally visible. Or you can use advanced scripting to globally add new build flags based on the existing build_flags option. But that would be more complicated since it can all be solved in the C/C++ code directly.
Strange. For example DEBUG macro used with no debug.h included in file. Actually debug.h for example has been invoked once from one of other files in this project. Is there any difference between ino and cpp files?
it first generates function prototypes from the .ino fiels to share them accross all other files.
So e.g. if you have these 3 files
main.ino
/* main sketch includes config */
#include "general.h"
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
You can see that the part_2.ino does not include general.h where SOME_DEBUG_FLAG is defined. Yet, when compiled, the following “error” confirms that it was defined:
This is due to when main.ino is pre-processed, the result is:
#include <Arduino.h>
#line 1 "C:\\Users\\Maxi\\Documents\\Arduino\\sketch_feb13a\\sketch_feb13a.ino"
/* main sketch includes config */
#include "general.h"
#line 4 "C:\\Users\\Maxi\\Documents\\Arduino\\sketch_feb13a\\sketch_feb13a.ino"
void setup();
#line 9 "C:\\Users\\Maxi\\Documents\\Arduino\\sketch_feb13a\\sketch_feb13a.ino"
void loop();
#line 7 "C:\\Users\\Maxi\\Documents\\Arduino\\sketch_feb13a\\part_2.ino"
void someFunction();
#line 4 "C:\\Users\\Maxi\\Documents\\Arduino\\sketch_feb13a\\sketch_feb13a.ino"
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
#line 1 "C:\\Users\\Maxi\\Documents\\Arduino\\sketch_feb13a\\part_2.ino"
#ifdef SOME_DEBUG_FLAG
#error "SOME_DEBUG_FLAG defined!!!"
#else
#error "SOME_DEBUG_FLAG not defined!!"
#endif
void someFunction() {
return;
}
As you can see, all the .ino files hahve simply been concatinated after each other, starting at the main.ino, producing on giant .cpp file.
That’s why when you look at the debug.ino file, it doesn’t seem to be including anything (and still work), but under the hood the Arduino IDE has concatinated every ´ino, starting at the main espurna.ino`, which as a first act includes the configuration file, which makes the debug flags visible.
When you’re using PlatformIO it compiles the .cpp files as their own compilation unit (which is absolutely standard). So that side-effect of the Arduino IDE is not done there. PlatformIO can still do it though, you just have to use .ino files everywhere, too.
However, I would highly sugest to write proper C++ code in .cpp file, not Arduino’s hacky .ino file + preprocessing format.