我有一个嵌入式系统项目,我正在用Ceedling (=Unity和Cmock)进行测试。
在一个测试用例中,测试代码就是这么简单:
uint32_t zero = eeprom_read_dword((uint32_t*)&non_volatile_zero);
sprintf(output, "%lu", zero);由于嵌入式系统是8位体系结构,在sprintf中必须使用%lu来格式化32位无符号整数以进行打印。但是,桌面环境(GCC)用于测试构建和运行测试(不能选择使用嵌入式构建进行测试)。这将导致下一个警告:
warning: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument 3 has type ‘uint32_t’ {aka ‘unsigned int’} [-Wformat=]
62 sprintf(output, "%lu", zero);
~~^ ~~~~
| |
| uint32_t {aka unsigned int}
long unsigned int
%u警告本身在桌面环境中是正确的,但从嵌入式系统的角度来看是错误的。
我的问题是如何为测试版本设置-Wno-format编译器标志,因为我根本没有在project.yml中定义tools-section,因为默认使用的是GCC?或者甚至有一种方法可以告诉ceedling目标系统正在使用8位架构?
发布于 2021-10-06 06:28:06
与其寻找禁用警告的方法,不如处理警告所涉及的问题。也就是说,使用inttypes.h中的可移植格式说明符。这些是打印stdint.h类型时使用的最正确的方法。
#include <inttypes.h>
sprintf(output, "%"PRIu32, zero);发布于 2021-10-06 07:29:29
如果有人碰巧搜索到原始问题答案,这里是一个解决方案,如何为指定的源文件添加编译器标志,而不需要在project.yml中定义整个工具部分
# Adds -Wno-format for sourcefile.c
:flags:
:test:
:compile:
:sourcefile: # use :*: for all sources.
- -Wno-formathttps://stackoverflow.com/questions/69460722
复制相似问题