我如何才能最好地将' __attribute__ ((unused)) '应用于这些自动生成的对象

How best can I programmatically apply `__attribute__ ((unused))` to these auto-generated objects?

本文关键字:应用于 自动生成 对象 unused attribute      更新时间:2023-10-16

在我的makefile中,我有以下目标,它使用xxd -i将文本/HTML资源"编译"成unsigned char数组。

我将结果包装在一个匿名命名空间中,并在翻译单元内部和翻译单元之间使用头警卫来保证多包含安全。

templates.h:
    @echo "#ifndef TEMPLATES_H" > templates.h
    @echo "#define TEMPLATES_H" >> templates.h
    @echo "// Auto-generated file! Do not modify!" >> templates.h
    @echo "// NB: arrays are not null-terminated" >> templates.h
    @echo "// (anonymous namespace used to force internal linkage)" >> templates.h
    @echo "namespace {" >> templates.h
    @echo "namespace templates {" >> templates.h
    @cd templates;
    for i in * ;
    do 
        echo "Compiling $$i...";
        xxd -i $$i >> ../templates.h;
    done;
    cd ..
    @echo "}" >> templates.h
    @echo "}" >> templates.h
    @echo "#endif" >> templates.h

输出,如果我只有一个这样的资源,看起来像这样(实际内容编辑):

#ifndef TEMPLATES_H
#define TEMPLATES_H
// Auto-generated file! Do not modify!
// NB: arrays are not null-terminated
// (anonymous namespace used to force internal linkage)
namespace {
namespace templates {
unsigned char alert_email_finished_events_html[] = {
  0x3c, 0x74, 0x61, 0x62, 0x6c, 0x0d, 0x0a
};
unsigned int alert_email_finished_events_html_len = 7;
}
}
#endif

以编程方式将GCC的__attribute__ ((unused))应用于这些字符数组的最佳方法是什么?我不希望GCC警告我在任何给定的TU中最终没有使用的任何资源,但我也不想完全关闭"未使用的变量"警告。

我认为快速sed应该工作,考虑到xxd -i的输出是非常规则的:

xxd -i $$i | sed -e 's/ =/ __attribute__((unused)) =/' >> ../templates.h