3

在我的makefile我有以下目标,它unsigned char使用xxd -i.

我将结果包装在匿名名称空间和标题保护中,以确保翻译单元内部和之间的多重包含安全。

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

4

1 回答 1

2

我认为快速sed应该可以工作,因为输出xxd -i非常有规律:

xxd -i $$i | sed -e 's/ =/ __attribute__((unused)) =/' >> ../templates.h
于 2011-10-05T11:29:53.867 回答