Don't use 'index' as a variable name! (STM32 quirk)

I was trying to build some moderately complex demonstration code today and was unsurprised when it failed on the first try with an error (error: 'volatile uint8_t index' redeclared as different kind of symbol) However, the code seemed fine in that respect - no re-declarations there - so I delved into the error itself.

It turns out that this has come up before, yet from what I can see it’s never been diagnosed here (though at least one person noticed the particular problem):

In fact, it appears that ‘index’ is a name best avoided when writing code here (certainly in the IDE, probably no matter how you build the code using this framework).

Here’s a slightly more compact code example:

volatile uint8_t index = 0;

void setup() {

void loop() {

As long as you call ‘index’ something else (e.g., ‘index1’) this little demo - and the more complex demo I was trying originally - builds just fine.

It is worth noting that once you know what to look for, this quirk of the STM32 is not widely known but has been recognized elsewhere.

The fact that ‘index’ (and perhaps other names beyond the usual keywords) are best avoided should be called out in the documentation in a “porting” or better yet “quirks” section.

FWIW, here’s the raw build output for the error:

Processing  /workspace/bugtest.ino
make -C ../modules/photon/user-part all
make[1]: Entering directory '/firmware/modules/photon/user-part'
make -C ../../../user 
make[2]: Entering directory '/firmware/user'
Building cpp file: /workspace/bugtest.cpp
Invoking: ARM GCC CPP Compiler
mkdir -p ../build/target/user/platform-6-m/workspace/
arm-none-eabi-gcc -DSTM32_DEVICE -DSTM32F2XX -DPLATFORM_THREADING=1 -DPLATFORM_ID=6 -DPLATFORM_NAME=photon -DUSBD_VID_SPARK=0x2B04 -DUSBD_PID_DFU=0xD006 -DUSBD_PID_CDC=0xC006 -DSPARK_PLATFORM -g3 -gdwarf-2 -Os -mcpu=cortex-m3 -mthumb -DINCLUDE_PLATFORM=1 -DPRODUCT_ID=6 -DPRODUCT_FIRMWARE_VERSION=65535 -DUSE_STDPERIPH_DRIVER -DDFU_BUILD_ENABLE -DPARTICLE_NO_ARDUINO_COMPATIBILITY=0 -DSYSTEM_VERSION_STRING=0.6.1 -DRELEASE_BUILD -I./inc -I../wiring/inc -I../system/inc -I../services/inc -I../communication/src -I../hal/inc -I../hal/shared -I../hal/src/photon -I../hal/src/stm32f2xx -I../hal/src/stm32 -I../hal/src/photon/api -I../platform/shared/inc -I../platform/MCU/STM32F2xx/STM32_USB_Host_Driver/inc -I../platform/MCU/STM32F2xx/STM32_StdPeriph_Driver/inc -I../platform/MCU/STM32F2xx/STM32_USB_OTG_Driver/inc -I../platform/MCU/STM32F2xx/STM32_USB_Device_Driver/inc -I../platform/MCU/STM32F2xx/SPARK_Firmware_Driver/inc -I../platform/MCU/shared/STM32/inc -I../platform/MCU/STM32F2xx/CMSIS/Include -I../platform/MCU/STM32F2xx/CMSIS/Device/ST/Include -I../dynalib/inc -I/workspace/ -I./libraries -I/workspace/ -I/workspace/ -I/workspace/ -I/workspace/ -I. -MD -MP -MF ../build/target/user/platform-6-m/workspace/bugtest.o.d -ffunction-sections -fdata-sections -Wall -Wno-switch -Wno-error=deprecated-declarations -fmessage-length=0 -fno-strict-aliasing -DSPARK=1 -DPARTICLE=1 -DSTART_DFU_FLASHER_SERIAL_SPEED=14400 -DSTART_YMODEM_FLASHER_SERIAL_SPEED=28800 -DSPARK_PLATFORM_NET=BCM9WCDUSI09 -fno-builtin-malloc -fno-builtin-free -fno-builtin-realloc  -DLOG_INCLUDE_SOURCE_INFO=1 -DPARTICLE_USER_MODULE -DUSE_THREADING=0 -DUSE_SPI=SPI -DUSE_CS=A2 -DUSE_SPI=SPI -DUSE_CS=A2 -DUSE_THREADING=0 -DUSER_FIRMWARE_IMAGE_SIZE=0x20000 -DUSER_FIRMWARE_IMAGE_LOCATION=0x80A0000 -DMODULAR_FIRMWARE=1 -DMODULE_VERSION=4 -DMODULE_FUNCTION=5 -DMODULE_INDEX=1 -DMODULE_DEPENDENCY=4,2,105 -D_WINSOCK_H -D_GNU_SOURCE -DLOG_MODULE_CATEGORY="\"app\""  -fno-exceptions -fno-rtti -fcheck-new -std=gnu++11 -c -o ../build/target/user/platform-6-m/workspace/bugtest.o /workspace/bugtest.cpp
/workspace/bugtest.cpp:2:18: error: 'volatile uint8_t index' redeclared as different kind of symbol
 #line 1
In file included from /usr/local/gcc-arm-embedded/arm-none-eabi/include/string.h:10:0,
                 from ../services/inc/logging.h:121,
                 from ../services/inc/service_debug.h:34,
                 from ../services/inc/debug.h:1,
                 from ../wiring/inc/spark_wiring.h:36,
                 from ./inc/application.h:36,
                 from /workspace/bugtest.cpp:1:
/usr/local/gcc-arm-embedded/arm-none-eabi/include/string.h:55:8: error: previous declaration of 'char* index(const char*, int)'
 char  *_EXFUN(index,(const char *, int));
../build/ recipe for target '../build/target/user/platform-6-m/workspace/bugtest.o' failed
make[2]: Leaving directory '/firmware/user'
make[2]: *** [../build/target/user/platform-6-m/workspace/bugtest.o] Error 1
../../../build/ recipe for target 'user' failed
make[1]: Leaving directory '/firmware/modules/photon/user-part'
make[1]: *** [user] Error 2
../build/ recipe for target 'modules/photon/user-part' failed
make: *** [modules/photon/user-part] Error 2
1 Like

And not even a day later, on a completely different project: same exact problem. So watch out…