c++ - 如何最好地以编程方式将 `__attribute__ ((unused))` 应用于这些自动生成的对象?

标签 c++ gcc makefile xxd

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

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

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

最佳答案

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

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

关于c++ - 如何最好地以编程方式将 `__attribute__ ((unused))` 应用于这些自动生成的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7660494/

相关文章:

c++ - 将 goto 标签暴露给符号表

c - 是否可以通过 makefile 设置环境变量?

powershell - 文件时间戳可以用于定义 Psake PowerShell makefile 中的依赖项吗?

c++ - 在主函数之外退出程序

c++ - 如何将我的 makefile 项目导入 QtCreator?

linux - 冒泡排序时间C程序

c++ - RInside 和 QtCreator

c++ - 函数调用约定

c++ - 与 DrawText 函数冲突

macos - 未链接的 clang - 编译器错误 (MacOS)