This is a cool problem!
Yes, it's a bit of an odd one that's for sure!
Are there any compiler warnings? Try compiling with "-O1 -Wall". There are some warnings that won't show up if the optimizer is off.
Ok, still no warnings. -O1 seems to 'fix' the problem. Here is the build log:
arm-none-eabi-gcc.exe -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -D__HEAP_SIZE=0x0000 -D__STACK_SIZE=0x0100 -mfloat-abi=hard -fno-strict-aliasing -Wextra -Wall -fdata-sections -ffunction-sections -O1 -g3 -DARM_MATH_CM4 -D__FPU_USED -DSTM32F407VG -DSTM32F4XX -DUSE_STDPERIPH_DRIVER -c src\startup_stm32f4xx.S -o obj\debug\src\startup_stm32f4xx.o -MMD -I.\inc -I.\src -I.\cmsis -I.\SPL\inc -I.\SPL\src -ID:\Development\tools\EmBitz\1.11\share\em_armgcc\bin\..\arm-none-eabi\include -ID:\Development\tools\EmBitz\1.11\share\em_armgcc\bin\..\arm-none-eabi -ID:\Development\tools\EmBitz\1.11\share\em_armgcc\bin\..\lib\cmsis\include
arm-none-eabi-gcc.exe -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -D__HEAP_SIZE=0x0000 -D__STACK_SIZE=0x0100 -mfloat-abi=hard -fno-strict-aliasing -Wextra -Wall -fdata-sections -ffunction-sections -O1 -g3 -DARM_MATH_CM4 -D__FPU_USED -DSTM32F407VG -DSTM32F4XX -DUSE_STDPERIPH_DRIVER -c src\system_stm32f4xx.c -o obj\debug\src\system_stm32f4xx.o -MMD -I.\inc -I.\src -I.\cmsis -I.\SPL\inc -I.\SPL\src -ID:\Development\tools\EmBitz\1.11\share\em_armgcc\bin\..\arm-none-eabi\include -ID:\Development\tools\EmBitz\1.11\share\em_armgcc\bin\..\arm-none-eabi -ID:\Development\tools\EmBitz\1.11\share\em_armgcc\bin\..\lib\cmsis\include
arm-none-eabi-gcc.exe -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -D__HEAP_SIZE=0x0000 -D__STACK_SIZE=0x0100 -mfloat-abi=hard -fno-strict-aliasing -Wextra -Wall -fdata-sections -ffunction-sections -O1 -g3 -DARM_MATH_CM4 -D__FPU_USED -DSTM32F407VG -DSTM32F4XX -DUSE_STDPERIPH_DRIVER -c src\main.c -o obj\debug\src\main.o -MMD -I.\inc -I.\src -I.\cmsis -I.\SPL\inc -I.\SPL\src -ID:\Development\tools\EmBitz\1.11\share\em_armgcc\bin\..\arm-none-eabi\include -ID:\Development\tools\EmBitz\1.11\share\em_armgcc\bin\..\arm-none-eabi -ID:\Development\tools\EmBitz\1.11\share\em_armgcc\bin\..\lib\cmsis\include
arm-none-eabi-gcc.exe -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -Wl,--defsym=__HEAP_SIZE=0x0000 -Wl,--defsym=__STACK_SIZE=0x0100 -mfloat-abi=hard -fno-strict-aliasing -Wextra -Wall -fdata-sections -ffunction-sections -O1 -g3 -Wl,-script="./stm32f407vg_sram.ld" -o bin\Debug\semihost1.elf obj\debug\src\startup_stm32f4xx.o obj\debug\src\system_stm32f4xx.o obj\debug\src\main.o -Wl,-Map=bin\Debug\semihost1.map -specs=nano.specs -Wl,--gc-sections -specs=rdimon.specs -lrdimon
Process terminated with status 0 (0 minutes, 0 seconds)
I noticed in your code the first printf starts with a form-feed. Is that intentional? It shouldn't make a difference but is a bit unusual.
Yes - it has been recommended to flush the buffers and the console log window so you know where you are starting from. It's not essential and you could use fflush().
printf() returns an integer number of characters written. I would try checking the result and verifying it makes sense. That should let you know if the printf thinks it is executing properly in which case your bug is probably with the low level semihosting calls or the connection to the host, rather than anything going on in your main function
Hmm, good idea and where it gets even more interesting. In cases where the fprinted string gets correctly printed to the console, fprint() returns -1, when it doesn't work it returns the correct number of characters!
Even curioser... just added code to assign the return value of the last printf and now it 'works' whatever the contents of the last printf() - but it too returns -1! So the code layout is affecting the behaviour not just the literals.
Another test just ocurred to me: when it 'works' printf() returns -1 and errno is 0. When it doesn't errno is 22 - EINVAL?
I would look at the disassembly of the resulting .o files and see if you can make sense of the differences and how they relate to whether a given version works.
In the debugger, all cases are the same - a simple call to puts(). I may try stepping through the call chain to see if I can make any sense that way.
I would double check your toolchain configuration. Is it possible you are doing something like compiling against one set of headers and linking against a different library version?
I guess it's possible, but it will take time to check that thoroughly. I have recently installed Atollic Studio but I'd be surprised if that was the problem. I don't use semihosting very often so I don't know if it used to work reliably in Embitz. It wasn't a problem in the earlier release 'Emblocks'.