[技术问答] MDK编译抱错 expression must have a constant value,如何解决

[复制链接]
2101|3
 楼主| mintspring 发表于 2024-7-24 14:51 | 显示全部楼层 |阅读模式
  1. ..\ST7735S\st7735.c(283): error:  #268: declaration may not appear after executable statement in block
  2.     uint8_t data[] = { 0x00, x0, 0x00, x1};
  3. ..\ST7735S\st7735.c(283): error:  #28: expression must have a constant value
  4.     uint8_t data[] = { 0x00, x0, 0x00, x1};
  5. ..\ST7735S\st7735.c(283): error:  #28: expression must have a constant value
  6.     uint8_t data[] = { 0x00, x0, 0x00, x1};
  7. ..\ST7735S\st7735.c(324): error:  #28: expression must have a constant value
  8.                   uint8_t data[] = { color >> 8, color & 0xFF };
  9. ..\ST7735S\st7735.c(324): error:  #28: expression must have a constant value
  10.                   uint8_t data[] = { color >> 8, color & 0xFF };
  11. ..\ST7735S\st7735.c(329): error:  #28: expression must have a constant value
  12.                   uint8_t data[] = { bgColor >> 8, bgColor & 0xFF };
  13. ..\ST7735S\st7735.c(329): error:  #28: expression must have a constant value
  14.                   uint8_t data[] = { bgColor >> 8, bgColor & 0xFF };
  15. ..\ST7735S\st7735.c: 0 warnings, 7 errors
  16. ".\obj\MySPI.axf" - 7 Error(s), 0 Warning(s).
  17. Target not created.
  18. Build Time Elapsed:  00:00:01
报错这个如何解决?
 楼主| mintspring 发表于 2024-7-24 14:52 | 显示全部楼层
@gaoyang9992006,大佬发帖了, 快告诉我。
gaoyang9992006 发表于 2024-7-24 14:55 | 显示全部楼层
针对这些错误,您可以采取以下步骤来修改代码:

将变量声明移动到作用域的开头。 确保在函数或者代码块的开头先声明变量,然后再进行可执行语句。

处理数组初始化问题。 如果需要动态初始化数组,考虑使用动态内存分配(如 malloc() 函数)或者使用静态数组,并确保初始化时使用常量表达式。

另外就是这种是老版本的编译器导致的,在新的C标准是支持这种方式的。
gaoyang9992006 发表于 2024-7-24 14:56 | 显示全部楼层
4568166a0a59c25fda.png
比如你在工程的这个地方打钩,选择C99标准就可以了。你试试。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

303

主题

4972

帖子

24

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