Vscode and pio..linker error when debugging


I’m facing a weird problem when, using vscode and pio, I try to debug Marlin firmware.

Firmware is compiling correctly when clicking on build task, so platformio.ini is correct (or so I think)
but when I click for “start debugging” i get a linker error. This let me think that standard link options are different from debug ones…where do I find them?

Linker doesn’t find:
static constexpr uint8_t fan_speed_scaler[FAN_COUNT] = ARRAY_N(FAN_COUNT, 128, 128, 128, 128, 128, 128);

but changing source with:
static uint8_t fan_speed_scaler[FAN_COUNT];

works fine even in debug. I think it’s a optimization option but, again, where do are stored passed options? only in platformio.ini? There is nothing there that specialized for debug

Any help is appreciated, thanks

Full error message and log?

here full message:

Linking .pioenvs\adafruit_grandcentral_m4\firmware.elf
.pioenvs/adafruit_grandcentral_m4/src/src/module/planner.cpp.o: In function Planner::check_axes_activity()': c:\Users\Giuliano\Documents\GitHub\Marlin/Marlin\src\module/planner.cpp:1298: undefined reference to Temperature::fan_speed_scaler’
collect2.exe: error: ld returned 1 exit status
*** [.pioenvs\adafruit_grandcentral_m4\firmware.elf] Error 1

but don’t know where to get log…

In a PIO Terminal (or just cmd.exe if it has access to PIO commands)

pio run -t clean
pio run -v > normal_compile.log 2>&1
pio debug -v > debug_compile.log 2>&1

Should create the log files. You might have to kill the second command after some time if it doesn’t terminate but wait for imput, after it has filled the log.

I can’t upload log files but I can see that in gcc differences are:
-g -ggdb -Os
-g -ggdb -Og -g3 -ggdb3 -D__PLATFORMIO_BUILD_DEBUG__

Does the same error occur during normal build when you add

build_unflags = -Os
build_flags = -Og

to the platformio.ini?

-Og is the problem.
I found with google what -Os does but can’t find what -Og. Can be safely removed when debugging?

-Og is optimize for debugging (Optimize Options (Using the GNU Compiler Collection (GCC))), -Os is optimize for size (there’s also -O0, -O1, -O2, -O3 for various degrees of code optimization. Which code optimization flags break the code?

This is kind of strange thing and has maybe to do with the fact that the code is using constexpr whose compilation result is dependent on the optimzation level? Not sure yet. Does just removing constexpr get good results for each compile?

Yes, that was one of my first test…I think marlin uses constexpr to optimize code/memory use. I can’t remove for sure constexpr…maybe I can change it only in debug

edit: removing constexpr works, and this was my first test

@maxgerhardt thanks for your help