同文件单个函数出现链接器undefined reference错误求助
同一FatFs源文件下f_mount正常,f_mkfs触发「undefined reference」错误
问题详情
头文件ff.h声明了两个函数:
// ff.h FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); FRESULT f_mkfs (const TCHAR* path, const MKFS_PARM* opt, void* work, UINT len);
对应的源文件ff.c实现了这两个函数:
// ff.c #include "ff.h" FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt) {...} FRESULT f_mkfs (const TCHAR* path, const MKFS_PARM* opt, void* work, UINT len) {...}
主文件中调用时,f_mount可以正常工作,但f_mkfs触发链接错误:
#include "ff.h" [...] fresult = f_mount(&fs, "0:", 1); [...] fresult = f_mkfs("0:", &fmt_opt, work, sizeof work);
编译输出如下:
>>> output: 11:24:45 **** Incremental Build of configuration Debug for project laser_manual **** make -j8 all arm-none-eabi-gcc -o "laser_manual.elf" @"objects.list" -mcpu=cortex-m4 -T"C:\Users\Pesquisa2\Documents\stm32\LM_STM32\STM32F407ZGTX_FLASH.ld" --specs=nosys.specs -Wl,-Map="laser_manual.map" -Wl,--gc-sections -static --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -u _printf_float -u _scanf_float -Wl,--start-group -lc -lm -Wl,--end-group c:\st\stm32cubeide_1.9.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.10.3-2021.10.win32_1.0.0.202111181127\tools\arm-none-eabi\bin\ld.exe: ./Src/main.o: in function `main': C:/Users/Pesquisa2/Documents/stm32/LM_STM32/Debug/../Src/main.c:55: undefined reference to `f_mkfs' collect2.exe: error: ld returned 1 exit status make: *** [makefile:68: laser_manual.elf] Error 1 "make -j8 all" terminated with exit code 2. Build might be incomplete.
环境配置
路径设置(STM32CubeMX)
在项目属性C/C++ General/Paths and Symbols中配置:
- 包含路径:
/laser_manual/FATFS/inc - 源文件路径:
/laser_manual/FATFS
工具参数
MCU GCC汇编器
-mcpu=cortex-m4 -g3 -DDEBUG -c -I"C:\Users\Pesquisa2\Documents\stm32\LM_STM32\FATFS\src" -x assembler-with-cpp --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb
MCU GCC编译器
-mcpu=cortex-m4 -std=gnu11 -g3 -DDEBUG -DSTM32 -DSTM32F4 -DSTM32F407ZGTx -c -I"C:\Users\Pesquisa2\STM32Cube\Repository\STM32Cube_FW_F4_V1.27.0\Drivers\CMSIS\Include" -I"C:\Users\Pesquisa2\STM32Cube\Repository\STM32Cube_FW_F4_V1.27.0\Drivers\CMSIS\Device\ST\STM32F4xx\Include" -I"C:\Users\Pesquisa2\Documents\stm32\LM_STM32\Inc\App" -I"C:\Users\Pesquisa2\Documents\stm32\LM_STM32\Inc\HAL" -I"C:\Users\Pesquisa2\Documents\stm32\LM_STM32\Inc" -I"C:\Users\Pesquisa2\Documents\stm32\LM_STM32\Src\Midware" -I"C:\Users\Pesquisa2\Documents\stm32\LM_STM32\Src\HAL" -I"C:\Users\Pesquisa2\Documents\stm32\LM_STM32\Src\App" -I"C:\Users\Pesquisa2\Documents\stm32\LM_STM32\Inc\Midware" -I"C:\Users\Pesquisa2\Documents\stm32\LM_STM32\FATFS\inc" -O0 -ffunction-sections -fdata-sections -Wall -fstack-usage --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb
MCU GCC链接器
-mcpu=cortex-m4 -T"C:\Users\Pesquisa2\Documents\stm32\LM_STM32\STM32F407ZGTX_FLASH.ld" --specs=nosys.specs -Wl,-Map="${BuildArtifactFileBaseName}.map" -Wl,--gc-sections -static --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -u _printf_float -u _scanf_float -Wl,--start-group -lc -lm -Wl,--end-group
问题原因及解决方法
核心原因
Elm Chan的FatFs库通过条件编译宏控制功能开关,f_mkfs默认可能未被编译:
- 如果
_FS_READONLY被设为1,库会禁用所有写操作相关功能,包括f_mkfs - 如果
_USE_MKFS未设为1,f_mkfs的实现会被预编译指令跳过,不会生成目标代码
解决步骤
- 检查FatFs配置文件
ffconf.h:- 确保
_FS_READONLY定义为0(允许写操作) - 确保
_USE_MKFS定义为1(启用格式化功能)
- 确保
- 验证编译宏是否生效:检查编译器参数中是否包含
-D_USE_MKFS=1等定义,避免配置文件中的宏被工程全局定义覆盖 - 清理并重新编译:删除Debug目录下的所有生成文件,执行全量编译,避免增量编译的缓存问题
- 确认目标文件包含f_mkfs符号:使用
arm-none-eabi-nm工具查看ff.o文件,确认存在f_mkfs符号
内容的提问来源于stack exchange,提问作者mgmussi
相关产品推荐
相关产品推荐

