[Security] 向STM32CubeIDE项目添加源文件时遇到的问题

[复制链接]
 楼主| 柴库柴库 发表于 2025-7-24 15:24 | 显示全部楼层 |阅读模式
本帖最后由 柴库柴库 于 2025-7-28 11:05 编辑

将一个包含简单测试函数的.cpp文件添加到Core/Src文件夹中,"build"过程似乎能正常处理,但无法从另一个源文件中访问该函数。
在main.c中声明外部函数不会引发错误
extern plus_one (int) ; // 不会导致问题
但在main()函数内部使用时
int x = plus_one (2); // 会导致编译失败

11:20:23 **** Incremental Build of configuration Debug for project TemplateL432KC_DMA ****
make -j4 all
arm-none-eabi-g++ "../Core/Src/tesnewfil.cpp" -mcpu=cortex-m4 -std=gnu++14 -g3 -DDEBUG -DUSE_HAL_DRIVER -DSTM32L432xx -c -I../Core/Inc -I../Drivers/STM32L4xx_HAL_Driver/Inc -I../Drivers/STM32L4xx_HAL_Driver/Inc/Legacy -I../Drivers/CMSIS/Device/ST/STM32L4xx/Include -I../Drivers/CMSIS/Include -O0 -ffunction-sections -fdata-sections -fno-exceptions -fno-rtti -fno-use-cxa-atexit -Wall -fstack-usage -MMD -MP -MF"Core/Src/tesnewfil.d" -MT"Core/Src/tesnewfil.o" --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -o "Core/Src/tesnewfil.o"
arm-none-eabi-g++ -o "TemplateL432KC_DMA.elf" @"objects.list"  -mcpu=cortex-m4 -T"C:\STMCube\TemplateL432KC_DMA\STM32L432KCUX_FLASH.ld" --specs=nosys.specs -Wl,-Map="TemplateL432KC_DMA.map" -Wl,--gc-sections -static --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -Wl,--start-group -lc -lm -lstdc++ -lsupc++ -Wl,--end-group
c:\st\stm32cubeide_1.6.1\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: Core/Src/main.o: in function `main':
C:/STMCube/TemplateL432KC_DMA/Debug/../Core/Src/main.c:298: undefined reference to `plus_one'
collect2.exe: error: ld returned 1 exit status
make: *** [makefile:80: TemplateL432KC_DMA.elf] Error 1
"make -j4 all" terminated with exit code 2. Build might be incomplete

捉虫天师 发表于 2025-7-25 12:22 | 显示全部楼层
头文件的路径要包含进来。
elephant00 发表于 2025-7-28 16:33 | 显示全部楼层
C和C++的名称修饰机制不兼容。
jcky001 发表于 2025-7-28 19:33 | 显示全部楼层
在C++文件中用extern "C"包裹函数。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

48

主题

68

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部

48

主题

68

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部