硬汉嵌入式论坛

 找回密码
 立即注册
查看: 398|回复: 4
收起左侧

[MDK] Keil uVision V5.26.0.0 宏定义解析是否存在 bug?

[复制链接]

0

主题

6

回帖

6

积分

新手上路

积分
6
发表于 2026-5-29 15:55:26 | 显示全部楼层 |阅读模式
本帖最后由 xbin 于 2026-5-29 16:01 编辑

keil 在 `Options for Target -> C/C++ -> Define` 处定义宏 CONFIG_A, CONFIG_FILE=\"config.h\", CONFIG_B
其中我发现 CONFIG_B 并没有被定义,好像是没有被解析出来。
如果 CONFIG_B 定义在 CONFIG_FILE=\"config.h\" 前面就没有问题
我看 mbedtls 中就是这种写法

[C] 纯文本查看 复制代码
// Middlewares/Third_Party/mbedTLS/include/mbedtls/build_info.h
/* X.509, TLS and non-PSA crypto configuration */
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/mbedtls_config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif


下面是测试用例:

[C] 纯文本查看 复制代码
#if defined(CONFIG_A)
int a[-1];
#endif

#include CONFIG_FILE

#if defined(CONFIG_B)
int b[-1];
#endif

int main() {    
    return 0;
}

/*
uVision V5.26.0.0

1. CONFIG_A, CONFIG_B, CONFIG_FILE=\"config.h\"

```txt
Build target 'A_B_FILE'
compiling main.c...
main.c(2): error:  #94: the size of an array must be greater than zero
  int a[-1];
main.c(8): error:  #94: the size of an array must be greater than zero
  int b[-1];
main.c: 0 warnings, 2 errors
".\Objects\keil_test.axf" - 2 Error(s), 0 Warning(s).
```


2. CONFIG_A, CONFIG_FILE=\"config.h\", CONFIG_B

```txt
Build target 'A_FILE_B'
compiling main.c...
main.c(2): error:  #94: the size of an array must be greater than zero
  int a[-1];
main.c: 0 warnings, 1 error
".\Objects\keil_test.axf" - 1 Error(s), 0 Warning(s).
```
*/




keil_test.rar

14.07 KB, 下载次数: 3

回复

使用道具 举报

1万

主题

7万

回帖

12万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
123173
QQ
发表于 2026-5-30 00:08:43 | 显示全部楼层
楼主提供的工程代码比较简单,没法直接编译帮你测试下

比如下面这个,缺少xxx.S启动文件
234.png
回复

使用道具 举报

0

主题

6

回帖

6

积分

新手上路

积分
6
 楼主| 发表于 2026-5-30 07:47:37 | 显示全部楼层
int a[-1]; 和 int b[-1]; 不是会导致编译错误吗,为什么会进入链接阶段呢
回复

使用道具 举报

0

主题

6

回帖

6

积分

新手上路

积分
6
 楼主| 发表于 2026-5-30 08:21:19 | 显示全部楼层
我用 cubemx 生成了一个 stm32f103c6 的示例,这个工程可行吗


注意:每个 Target 都要用按重新编译 Rebuild,而不能按编译 Build(F7)


Target: keil_test 的宏:CONFIG_FILE=\"config.h\"

```txt
"keil_test\keil_test.axf" - 0 Error(s), 0 Warning(s).
```


Target: A_B_FILE 的宏:CONFIG_A, CONFIG_B, CONFIG_FILE=\"config.h\"

```txt
compiling main.c...
../Core/Src/main.c(25): error:  #94: the size of an array must be greater than zero
  int a[-1];
../Core/Src/main.c(31): error:  #94: the size of an array must be greater than zero
  int b[-1];
../Core/Src/main.c: 0 warnings, 2 errors
```

由于定义了 CONFIG_A 和 CONFIG_B 所有出现两个 error,是预期结果


Target: A_FILE_B 的宏:CONFIG_A, CONFIG_FILE=\"config.h\", CONFIG_B

```txt
compiling main.c...
../Core/Src/main.c(25): error:  #94: the size of an array must be greater than zero
  int a[-1];
../Core/Src/main.c: 0 warnings, 1 error
```

在当前顺序的情况下,从编译错误可以看出 CONFIG_B 没有被定义
这一点也可以从 .\Objects\keil_test_A_FILE_B.dep 文件中看出来

```txt
F (.\main.c)(0x6A19436A)(--c99 --gnu -c --cpu Cortex-M0 --li -g -O0 --apcs=interwork --split_sections

-I.\RTE\_A_FILE_B

-IC:\Keil_v5\ARM\PACK\ARM\CMSIS\5.9.0\Device\ARM\ARMCM0\Include

-IC:\Keil_v5\ARM\CMSIS\Include

-D__UVISION_VERSION="526" -DARMCM0 -DCONFIG_A -DCONFIG_FILE="\"config.h\""

-o .\objects\main.o --omf_browse .\objects\main.crf --depend .\objects\main.d)
```

keil_test.rar

10.74 MB, 下载次数: 1

回复

使用道具 举报

1万

主题

7万

回帖

12万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
123173
QQ
发表于 2026-5-30 15:19:15 | 显示全部楼层
xbin 发表于 2026-5-30 08:21
我用 cubemx 生成了一个 stm32f103c6 的示例,这个工程可行吗


注意:每个 Target 都要用按重新编译  ...

用MDK5.43 AC5和AC6都测试了,错误和你的一样,确实有这个问题
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2026-6-25 15:28 , Processed in 0.463863 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表