32K
An undefined reference to function error in STM32 IDEs comes up when there is a problem with the linker configuration. If header files are included and a certain function is used with no link to the actual source for that function, you will get the “undefined reference” to function error.
Cause of "Undefined Reference to Function" Error
There can be two causes of undefined reference:
- Missing C source file
A source file may be present in the project directory in a folder. It may even be present in the workspace explorer, but the linker needs to be instructed to link the object files generated from the C source files.
More on how to do this in the following section. - Missing library file
A library file (usually a .a file) contains pre compiled source code in the form of a linkable library. You need a header file and a library file, both present in your workspace and the library file must be known to the linker.
Advertisement (third-party):
Solution to "Undefined Reference to Function" Error
The solution is to make sure the linker knows of and can access the contents of the entire source tree. How this can be done varies across IDEs and toolchains.
For the STM32 CubeIDE based on Eclipse, here is what you can do to ensure that all source files and libraries are included in your build.
Make sure you do a “full clean” and then build the whole project after changing the source tree.
For Cube IDE:
- Open Project > Properties > C/C++ General > Paths and Symbols
- Make sure your active debug/run configuration is selected.
- Under the Source Location tab, Add Folder to make sure all folders that have sources are included in the list of source locations.
- Under the Library Paths tab, Add a library file if your project depends on one.
Need Help with STM32 Firmware?
Feel free to ask away via the Quick Contact form in the sidebar, or leave a comment below.
Feel free to ask away via the Quick Contact form in the sidebar, or leave a comment below.
Change Log
- Initial Release: 19 September 2021
References
- Reference 1: Stack Overflow thread on linker error
3 comments
Thank you so much! I spent so long looking for this solution and you explained it perfectly :)
Really Helpfull
Neat explanation.
Thanks for the fix.