使用 Ceedling 的编译器标志定义

Compiler flag definitions with Ceedling

我有一个嵌入式系统项目,我正在使用 Ceedling(=Unity 和 Cmock)对其进行测试。

在一个测试用例中,被测代码就是这么简单:

uint32_t zero = eeprom_read_dword((uint32_t*)&non_volatile_zero);
sprintf(output, "%lu", zero);

由于嵌入式系统是8位架构,所以在sprintf中必须使用%lu格式化32位unsigned int进行打印。但是,桌面环境 (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 中定义工具部分,因为使用默认 GCC?或者甚至有办法告诉 ceedling 目标系统正在使用 8 位架构?

与其寻找禁用警告的方法,不如处理警告所涉及的问题。也就是说,使用 inttypes.h 中的可移植格式说明符。这些是打印 stdint.h 类型时最正确的用法。

#include <inttypes.h>

sprintf(output, "%"PRIu32, zero);

如果有人碰巧搜索原始问题的答案,这里有一个解决方案如何为指定的源文件添加编译器标志,而无需在 project.yml 中定义整个工具部分:

# Adds -Wno-format for sourcefile.c
:flags:
  :test:
    :compile:
      :sourcefile: # use :*: for all sources.
        - -Wno-format