stm32f407通过cubemx移植freertos遇到一堆编译错误。

[复制链接]
2121|2
keer_zu 发表于 2025-9-4 10:40 | 显示全部楼层 |阅读模式


重新选择编译器和设置:(如附件)将 RVDS/ARM_CM4F 内的文件全部替换成GCC/ARM_CM4F里的文件。

切换编译器和参数:


8063668b8fbf706017.png

377568b8fbc1ccbd6.png



 楼主| keer_zu 发表于 2025-9-4 10:40 | 显示全部楼层
编译后错误减少:

  1. Build started: Project: lora_gtw
  2. *** Using Compiler 'V6.21', folder: 'd:\Keil_v5\ARM\ARMCLANG\Bin'
  3. Build target 'lora_gtw'
  4. ../Core/Src/usart.c(25): error: tentative definition has type 'FILE' (aka 'struct __FILE') that is never completed
  5.    25 | FILE __stdout;     
  6.       |      ^
  7. d:\Keil_v5\ARM\ARMCLANG\Bin\..\include\stdio.h(108): note: forward declaration of 'struct __FILE'
  8.   108 | typedef struct __FILE FILE;
  9.       |                ^
  10. 1 error generated.
  11. compiling usart.c...
  12. compiling freertos.c...
  13. compiling err.c...
  14. ../Middlewares/Third_Party/LwIP/src/api/if_api.c(49): warning: In file included from...
  15. ../Middlewares/Third_Party/LwIP/src/include\lwip/priv/sockets_priv.h(45): warning: In file included from...
  16. ../Middlewares/Third_Party/LwIP/src/include\lwip/sockets.h(601): warning: declaration of 'struct timeval' will not be visible outside of this function [-Wvisibility]
  17.   601 |                 struct timeval *timeout);
  18.       |                        ^
  19. 1 warning generated.
  20. compiling if_api.c...
  21. compiling lwip.c...
  22. compiling bridgeif.c...
  23. ../Core/Src/main.c(114): error: call to undeclared function 'MX_LWIP_Process'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
  24.   114 |                 MX_LWIP_Process();
  25.       |                 ^
  26. 1 error generated.
  27. compiling main.c...
  28. compiling bridgeif_fdb.c...
  29. compiling ethernetif.c...
  30. compiling api_lib.c...
  31. compiling api_msg.c...
  32. ../Middlewares/Third_Party/LwIP/src/core/init.c(47): warning: In file included from...
  33. ../Middlewares/Third_Party/LwIP/src/include\lwip/sockets.h(601): warning: declaration of 'struct timeval' will not be visible outside of this function [-Wvisibility]
  34.   601 |                 struct timeval *timeout);
  35.       |                        ^
  36. 1 warning generated.
  37. compiling init.c...
  38. ../Middlewares/Third_Party/LwIP/src/api/sockets.c(44): warning: In file included from...
  39. ../Middlewares/Third_Party/LwIP/src/include\lwip/sockets.h(601): warning: declaration of 'struct timeval' will not be visible outside of this function [-Wvisibility]
  40.   601 |                 struct timeval *timeout);
  41.       |                        ^
  42. ../Middlewares/Third_Party/LwIP/src/api/sockets.c(1965): warning: declaration of 'struct timeval' will not be visible outside of this function [-Wvisibility]
  43. 1965 |             struct timeval *timeout)
  44.       |                    ^
  45. ../Middlewares/Third_Party/LwIP/src/api/sockets.c(1964): error: conflicting types for 'lwip_select'
  46. 1964 | lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
  47.       | ^
  48. ../Middlewares/Third_Party/LwIP/src/include\lwip/sockets.h(600): note: previous declaration is here
  49.   600 | int lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
  50.       |     ^
  51. ../Middlewares/Third_Party/LwIP/src/api/sockets.c(2007): error: incomplete definition of type 'struct timeval'
  52. 2007 |     if (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0) {
  53.       |                    ~~~~~~~^
  54. ../Middlewares/Third_Party/LwIP/src/api/sockets.c(1965): note: forward declaration of 'struct timeval'
  55. 1965 |             struct timeval *timeout)
  56.       |                    ^
  57. ../Middlewares/Third_Party/LwIP/src/api/sockets.c(2007): error: incomplete definition of type 'struct timeval'
  58. 2007 |     if (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0) {
  59.       |                                            ~~~~~~~^
  60. ../Middlewares/Third_Party/LwIP/src/api/sockets.c(1965): note: forward declaration of 'struct timeval'
  61. 1965 |             struct timeval *timeout)
  62.       |                    ^
  63. ../Middlewares/Third_Party/LwIP/src/api/sockets.c(2082): error: incomplete definition of type 'struct timeval'
  64. 2082 |             long msecs_long = ((timeout->tv_sec * 1000) + ((timeout->tv_usec + 500) / 1000));
  65.       |                                 ~~~~~~~^
  66. ../Middlewares/Third_Party/LwIP/src/api/sockets.c(1965): note: forward declaration of 'struct timeval'
  67. 1965 |             struct timeval *timeout)
  68.       |                    ^
  69. ../Middlewares/Third_Party/LwIP/src/api/sockets.c(2082): error: incomplete definition of type 'struct timeval'
  70. 2082 |             long msecs_long = ((timeout->tv_sec * 1000) + ((timeout->tv_usec + 500) / 1000));
  71.       |                                                             ~~~~~~~^
  72. ../Middlewares/Third_Party/LwIP/src/api/sockets.c(1965): note: forward declaration of 'struct timeval'
  73. 1965 |             struct timeval *timeout)
  74.       |                    ^
  75. 2 warnings and 5 errors generated.
  76. compiling sockets.c...
  77. compiling tcpip.c...
  78. ../Middlewares/Third_Party/LwIP/src/core/memp.c(66): warning: In file included from...
  79. ../Middlewares/Third_Party/LwIP/src/include\lwip/priv/sockets_priv.h(45): warning: In file included from...
  80. ../Middlewares/Third_Party/LwIP/src/include\lwip/sockets.h(601): warning: declaration of 'struct timeval' will not be visible outside of this function [-Wvisibility]
  81.   601 |                 struct timeval *timeout);
  82.       |                        ^
  83. 1 warning generated.
  84. compiling memp.c...
  85. compiling mem.c...
  86. compiling sys.c...
  87. compiling timeouts.c...
  88. compiling netif.c...
  89. compiling sys_arch.c...
  90. compiling croutine.c...
  91. compiling pbuf.c...
  92. compiling list.c...
  93. compiling event_groups.c...
  94. compiling mqtt.c...
  95. compiling timers.c...
  96. compiling heap_4.c...
  97. compiling port.c...
  98. compiling stream_buffer.c...
  99. compiling queue.c...
  100. compiling cmsis_os.c...
  101. compiling tasks.c...
  102. "lora_gtw\lora_gtw.axf" - 7 Error(s), 5 Warning(s).
  103. Target not created.
  104. Build Time Elapsed:  00:00:03


 楼主| keer_zu 发表于 2025-9-4 10:42 | 显示全部楼层

现在主要剩下两个问题需要解决:

  1. LWIP 的 struct timeval 冲突问题
  2. **MX_LWIP_Process() 函数未声明**
  3. **FILE __stdout 类型不完整的问题**

让我为您提供完整的解决方案:

解决方案

1. 解决 LWIP 的 struct timeval 冲突

lwipopts.h 文件中添加以下定义:

/* 在 lwipopts.h 文件的开头添加 */
#define LWIP_TIMEVAL_PRIVATE 1

#ifndef _SYS_TIME_H_
struct timeval {
    long tv_sec;
    long tv_usec;
};
#endif

或者,如果您想使用系统定义,可以尝试包含系统头文件:

#if __ARMCC_VERSION >= 6010050
#include <sys/time.h>
#else
struct timeval {
    long tv_sec;
    long tv_usec;
};
#endif

2. 解决 MX_LWIP_Process 未声明的问题

main.h 文件中添加函数声明:

/* 在 USER CODE BEGIN EFP 部分添加 */
void MX_LWIP_Process(void);

3. 解决 FILE __stdout 类型不完整的问题

这个问题是因为 ARMCLANG 对标准库的实现与 ARMCC5 不同。您需要重定向标准输出:

usart.c 中,替换 FILE __stdout; 的定义:

// 删除这一行: FILE __stdout;

// 添加以下代码实现标准输出重定向
#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
// ARMCLANG 版本的标准输出重定向
int _write(int file, char *ptr, int len)
{
    HAL_UART_Transmit(&huart1, (uint8_t*)ptr, len, 1000);
    return len;
}
#else
// 原有代码保持不变
FILE __stdout;
// ... 原有的 fputc 实现
#endif

4. 确保编译器设置正确

根据您提供的图片,您的编译器设置基本正确,但需要确认以下几点:

  1. C 语言标准: gnu11 (正确)
  2. 优化级别: -O2 (正确)
  3. FPU 设置: 确保已启用
    • -mcpu=cortex-m4 (正确)
    • -mfpu=fpv4-sp-d16 (正确)
    • -mfloat-abi=hard (正确)

5. 防止 CubeMX 覆盖的永久解决方案

创建 post_generate.sh 脚本:

#!/bin/bash
# 修复 LWIP 的 timeval 问题
if ! grep -q "LWIP_TIMEVAL_PRIVATE" Middlewares/Third_Party/LwIP/src/include/lwip/lwipopts.h; then
    echo -e "\n#define LWIP_TIMEVAL_PRIVATE 1\n#ifndef _SYS_TIME_H_\nstruct timeval {\n    long tv_sec;\n    long tv_usec;\n};\n#endif" >> Middlewares/Third_Party/LwIP/src/include/lwip/lwipopts.h
fi

# 添加 MX_LWIP_Process 声明
if ! grep -q "void MX_LWIP_Process(void);" Core/Inc/main.h; then
    echo "void MX_LWIP_Process(void);" >> Core/Inc/main.h
fi

# 修复 ARMCLANG 的标准输出重定向
if grep -q "FILE __stdout;" Core/Src/usart.c; then
    sed -i 's/FILE __stdout;//g' Core/Src/usart.c
    echo -e "#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)\nint _write(int file, char *ptr, int len)\n{\n    HAL_UART_Transmit(&huart1, (uint8_t*)ptr, len, 1000);\n    return len;\n}\n#else\nFILE __stdout;\n#endif" >> Core/Src/usart.c
fi

在 CubeMX 中设置后生成命令:
Project -> Advanced Settings -> Post Generate Command: bash ./post_generate.sh

验证步骤

  1. 应用上述修改
  2. 执行完全清理:Project -> Clean
  3. 重新编译:Project -> Rebuild All
  4. 检查是否还有编译错误

总结

已经完成了最困难的部分 - 将 FreeRTOS 移植层从 RVDS 切换到 GCC。剩下的问题主要是 LWIP 配置和 ARMCLANG 与 ARMCC5 的兼容性问题。通过上述解决方案,您应该能够解决所有编译错误。

关键点:

  1. LWIP 需要明确处理 struct timeval 的定义
  2. ARMCLANG 的标准库实现与 ARMCC5 不同,需要适配
  3. 所有函数都需要正确定义和声明
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:qq群:49734243 Email:zukeqiang@gmail.com

1488

主题

12949

帖子

55

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