3

In my makefile I have the following target, which "compiles" text/HTML resources into unsigned char arrays using xxd -i.

I wrap the result in an anonymous namespace and header guards for multiple-inclusion safety both inside and between translation units.

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

The output, if I had just one such resource, looks like this (actual content redacted):

#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

What would be the best way to programmaticaly apply GCC's __attribute__ ((unused)) to these character arrays? I don't want GCC warning about any resources that I end up not using in any given TU, but I also don't want to turn "unused variable" warnings off altogether.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Any reason why you don't compile using `-fvisibility=hidden` and only export those symbols you do want to have external linkage ? This way you could avoid including the templates everywhere. – Matthieu M. Oct 05 '11 at 12:32
  • @MatthieuM. Seems cumbersome since I want external linkage by default everywhere else. I want an approach that affects just the templates, and nothing else. I'm not too worried about duplicating the templates everywhere for the time being as I'm only using them in one TU; this approach simply provides error mitigation for when someone comes and `#include`s `template.h` in another TU for the first time. If it eventually ends up in lots of TUs then, sure, something cleverer may be worth devising. – Lightness Races in Orbit Oct 05 '11 at 12:43
  • @MatthieuM. (The other answer is that I don't yet completely "get" `-fvisibility="hidden"`. :P) – Lightness Races in Orbit Oct 05 '11 at 12:44
  • Another solution would be to selectively apply `__attribute__ ((visibility ("hidden"))` to those symbols then, instead of marking them as `unused`. The issue I have with unused is that a symbol used *but* marked as unused may cause warnings (if not immediately, perhaps in the future). – Matthieu M. Oct 05 '11 at 12:51
  • @MatthieuM.: Really? The [doc](http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#Function-Attributes) says "This attribute, attached to a function, means that the function is meant to be **possibly** unused. GCC will not produce a warning for this function." – Lightness Races in Orbit Oct 05 '11 at 12:58
  • @MatthieuM.: So I'd use "hidden" visibility and get rid of the anonymous namespace, and I gain sharing of identical objects across modules? – Lightness Races in Orbit Oct 05 '11 at 13:00
  • I think it was suggested on Clang, may have been abandonned though if GCC never warns. It is still misleading, of course, but then for generated code it does not matter as much. – Matthieu M. Oct 05 '11 at 13:00
  • @MatthieuM.: Indeed, it's more like `maybe-unused` :D – Lightness Races in Orbit Oct 05 '11 at 13:02

1 Answers1

2

I think a quick sed should work, given that the output of xxd -i is very regular:

xxd -i $$i | sed -e 's/ =/ __attribute__((unused)) =/' >> ../templates.h
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Mat
  • 202,337
  • 40
  • 393
  • 406