I’m having trouble declaring global C++ objects. Take the following code:
#include <vector>
std::vector<ztimer_periodic_t> timers2;
int main() {
return 0;
}
When compiled with the following Makefile
FEATURES_REQUIRED += periph_timer
FEATURES_REQUIRED += cpp libstdcpp
USEMODULE += ztimer_usec ztimer_msec ztimer_sec cxx_ctor_guards
The errors produced
/usr/lib/gcc/arm-none-eabi/12.2.1/../../../arm-none-eabi/bin/ld: /home/nathaniel/projects/riot/garden/bin/nucleo-f446re/application_garden/main.o: in function `_GLOBAL__sub_I_timers2':
/home/nathaniel/projects/riot/garden/main.cpp:3: undefined reference to `__dso_handle'
I have read that this is caused by guard code for constructors and the documentation says:
This module is intended to be used by platforms that want to provide C++ support, but the used standard C++ library does not provide these guards. In this case, adding this module will do the trick. The programmer / user should never interact with any of the functions.
Note that on some platforms the type
__guard
is defined differently from the “generic” definition, most notably ARM. For those platforms a header namedcxx_ctor_guards_arch.h
needs to be created containing the correcttypedef
and the preprocessor macroCXX_CTOR_GUARDS_CUSTOM_TYPE
needs to be defined.
I am working on a STM32-F446RE which is ARM based but I am at a loss as to how to do this. Help would be appreciated.
As a side note, based on some searching adding this line seems to do the trick but seems a dreadful hack:
void *__dso_handle __attribute__((weak)) = NULL;