Skip to main content
Associate
Apr 22, 2024 11:30 AM UTC
Question

STM32 VScode undefined reference HAL_FLASH_Unlock

Hello everyone,

I am currently taking my first steps with the STM32VScode extension. Previously I worked exclusively with the STM32cubeIDE.

I created a Cmake project via STM32cubeMX, which I then imported via the STM32 VScode extension. Everything worked fine. The build process afterwards was successful and I was able to run the program on my Nucleo-C31C6 board.

Now I want to access the flash memory. But as soon as I insert the function HAL_FLASH_Unlock() into my code, the following error occurs during the build process: main.c:109: undefined reference to `HAL_FLASH_Unlock'.

How do I have to include the HAL drivers correctly so that I can use the function?

 

maxl95

This topic has been closed for replies.

6 replies

Andrew Neil
Super User
Apr 22, 2024 11:43 AM UTC

So do you have a definition for HAL_FLASH_Unlock anywhere in your project?

 


@maxl95 wrote:

How do I have to include the HAL drivers correctly so that I can use the function?


As with any C code:

  1. You need to #include the appropriate header(s) in the file(s) where you call the function;
  2. You need to have the source file(s) which define the function - ie, contain its executable body - in your Project.
A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
maxl95Author
Associate
Apr 22, 2024 12:01 PM UTC

Hi Andrew, thanks for your quick replay,

  1. I've #include "stm32c0xx_hal_flash.h" in my main.c
  2. The source file is in the defaul directory --> ./Drivers/STM32C0xx_HAL_Driver/Src

I have also successfully used other functions (e.g. LL_USART) where the problem did not occur.

Andrew Neil
Super User
Apr 22, 2024 12:50 PM UTC

@maxl95 wrote:
  1.  
  2. The source file is in the defaul directory --> ./Drivers/STM32C0xx_HAL_Driver/Src

And is that file actually getting built?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Nawres GHARBI
ST Technical Moderator
Apr 25, 2024 8:41 AM UTC

Hello @maxl95 

Actually Flash drivers are by default integrated to your project

If you are adding HAL_FLASH_Unlock()  to a custom file you can include main.h or stm32c0xx_hal_conf.h to get flash functions available in your custom file

Visitor II
Mar 13, 2025 10:08 AM UTC

Hello @maxl95

I've just encountered this problem in my project but with STM32F4, and I'm also building with CMake. 

I've added the stm32f4xx_hal_flash.c path into my main CMakeLists.txt to PROJECT_SOURCES list along with files from Core/Src. And that works for me.