如何以编程方式将`__attribute __((unused))`应用于这些自动生成的对象?

时间:2011-10-05 11:22:16

标签: c++ gcc makefile xxd

在我的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中最终没有使用的任何资源,但我也不想完全关闭“未使用的变量”警告。

1 个答案:

答案 0 :(得分:2)

我认为快速sed应该有效,因为xxd -i的输出非常规律:

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