Skip to main content
RFeist
Associate
March 28, 2021
Solved

Enabling X-Cube-AI in STM32CubeIDE project causes "undefined reference to `__wrap_malloc'" errors during compile.

  • March 28, 2021
  • 6 replies
  • 4028 views

Steps to reproduce:

1.      Enable X-CUBE-AI core and add a network model.

2.      Go back into main.c and setup the calls to feed and run the model with data.

3.      Build the project.

Upon build, produces the following errors:

arm-none-eabi-gcc -o "NUCLEO-L476RG-Vibration_detect1.elf" @"objects.list" -larm_cortexM4lf_math -l:NetworkRuntime600_CM4_GCC.a -mcpu=cortex-m4 -T"C:\Users\feist\git\ROW_stationary_sensors\NUCLEO-L476RG-Vibration_detect1\STM32L476RGTX_FLASH.ld" --specs=nosys.specs -Wl,-Map="NUCLEO-L476RG-Vibration_detect1.map" -Wl,--gc-sections -static -Wl,--start-group -larm_cortexM4lf_math -l:NetworkRuntime600_CM4_GCC.a -Wl,--end-group -L../Middlewares/Third_Party/ARM_CMSIS/CMSIS/DSP/Lib/GCC -L../Middlewares/ST/AI/Lib -Wl,--wrap=malloc -Wl,--wrap=free -u _printf_float --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -Wl,--start-group -lc -lm -Wl,--end-group

c:\st\stm32cubeide_1.6.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.9-2020-q2-update.win32_1.5.0.202011040924\tools\arm-none-eabi\bin\ld.exe: c:/st/stm32cubeide_1.6.0/stm32cubeide/plugins/com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.9-2020-q2-update.win32_1.5.0.202011040924/tools/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard\libc_nano.a(lib_a-dtoa.o): in function `_dtoa_r':

dtoa.c:(.text._dtoa_r+0x24): undefined reference to `__wrap_malloc'

c:\st\stm32cubeide_1.6.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.9-2020-q2-update.win32_1.5.0.202011040924\tools\arm-none-eabi\bin\ld.exe: c:/st/stm32cubeide_1.6.0/stm32cubeide/plugins/com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.9-2020-q2-update.win32_1.5.0.202011040924/tools/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard\libc_nano.a(lib_a-mprec.o): in function `_Balloc':

mprec.c:(.text._Balloc+0xc): undefined reference to `__wrap_malloc'

c:\st\stm32cubeide_1.6.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.9-2020-q2-update.win32_1.5.0.202011040924\tools\arm-none-eabi\bin\ld.exe: c:/st/stm32cubeide_1.6.0/stm32cubeide/plugins/com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.9-2020-q2-update.win32_1.5.0.202011040924/tools/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard\libc_nano.a(lib_a-mprec.o): in function `_Bfree':

mprec.c:(.text._Bfree+0xc): undefined reference to `__wrap_malloc'

c:\st\stm32cubeide_1.6.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.9-2020-q2-update.win32_1.5.0.202011040924\tools\arm-none-eabi\bin\ld.exe: c:/st/stm32cubeide_1.6.0/stm32cubeide/plugins/com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.9-2020-q2-update.win32_1.5.0.202011040924/tools/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard\libc_nano.a(lib_a-mprec.o): in function `__pow5mult':

mprec.c:(.text.__pow5mult+0x2a): undefined reference to `__wrap_malloc'

collect2.exe: error: ld returned 1 exit status

make: *** [makefile:66: NUCLEO-L476RG-Vibration_detect1.elf] Error 1

"make -j8 all" terminated with exit code 2. Build might be incomplete.

It seems enabling X-CUBE-AI automatically adds the following GCC Linker flags in the C/C++ Build Settings of project properties:

-Wl,--wrap=malloc -Wl,--wrap=free

I can remove these flags to get a successful build, but then during runtime I get a hard fault when trying to run my ai model.  Seemingly because X-CUBE-AI needs these linked libraries that the compiler cant find.

I’ve tried reverting back to X-CUBE-AI (v5.2) but getting the same errors.

STM32CubeIDE (v1.6)

X-Cube-AI (v6.0.0)

Target board: Nucleo-L476RG

Targeted Language: C

This topic has been closed for replies.
Best answer by fauvarque.daniel

Thanks for reporting this issue, in fact the "-Wl,--wrap=malloc -Wl,--wrap=free" is only necessary to generate the system performance application.

When generating for the other case this option should not be put so you can safely remove it from the build.

You may want to increase the heap and stack size to avoid the hard fault.

X-Cube-AI automatically sets a heap and stack size to be able to run the network but of course doesn't take into account the rest of the application

Regards

Daniel

6 replies

fauvarque.daniel
fauvarque.danielBest answer
ST Employee
March 29, 2021

Thanks for reporting this issue, in fact the "-Wl,--wrap=malloc -Wl,--wrap=free" is only necessary to generate the system performance application.

When generating for the other case this option should not be put so you can safely remove it from the build.

You may want to increase the heap and stack size to avoid the hard fault.

X-Cube-AI automatically sets a heap and stack size to be able to run the network but of course doesn't take into account the rest of the application

Regards

Daniel

RFeist
RFeistAuthor
Associate
March 30, 2021

Thanks Daniel. All very helpful information. I was able to resolve my hard fault by reassessing my memory allocations throughout my program. As for the linker flags being added even when I don't have the system performance application enabled, yea I think this may be a bug in the current version of cubeMX within cubeIDE.

fauvarque.daniel
ST Employee
March 30, 2021

It is actually a bug in the X-CUBE-AI plugin of STM32CubeMX that will be fixed in the next release

DPiór.1
Associate III
April 22, 2021

Hi @fauvarque.daniel​ ,

I have the same issue. How can I remove those flags? "-Wl,--wrap=malloc -Wl,--wrap=free"

I can see them in project properties (C/C++ Build/Settings/MCU G++ Linker), but I can't edit it there.

STM32CubeIDE (v1.6.1)

X-Cube-AI (v6.0.0)

Target board: Nucleo-WB55

fauvarque.daniel
ST Employee
April 26, 2021

To remove the flags in STM32CubeIDE right click on the project and select "Properties"

Then select "C/C++ build -> Settings -> MCU GCC Linker -> Miscellaneous

In the Other flag you should see something like

-Wl,--wrap=malloc -Wl,--wrap=free -u _printf_float

You can edit this field and remove the -Wl,--wrap=malloc -Wl,--wrap=free, you can also remove the -u_printf_float if you don't plan to use printf with float values.

Regards

Daniel

DPiór.1
Associate III
April 28, 2021

Thanks Daniel!

I will check proposed solution if I will face this issue again.

I already created new project and don't have this issue anymore. I don't know why sometimes the problem appears and sometimes not.

fauvarque.daniel
ST Employee
April 28, 2021

I was only able to reproduce the issue by first generating with "validation" and then switch to "Application template"

Our strategy is to not touch the parameters of the project once it has been created once by STM32CubeMX, so in that case the parameters remain, so creating the issue.

This is a tricky one as if you actually need the wrap in your project you won't be happy if we remove it each time you generate again.

Regards

Daniel

DPiór.1
Associate III
May 6, 2021

I found a way how to remove those flags. I just can remove them directly from .cproject file.

Unfortunately I still didn't find the rule for this issue. In most cases it happens after editing .ioc file in CubeMX, but not always.