I’d like to include in na single static C program a bunch of data (say, images, but also other data, embedded in executable since I’m working on an embedded platform without files).
Thus, I wrote a little img2c creating const data from my data files, creating a file with static const arrays initializers to be put to flash (using C99 nice features)
My question is, should I put them in a .h file, like I’ve seen many times – by example gimp can save as .h files, not .c files – or in a .c file, referenced in a header with just the const extern declaration for further references, without having to include all data and pass it all to the compiler, and redeclare it each time I use it ?
Preprocessor macros are out of the question, since I’ll reference their address, not include the whole data each time.
Header files in C are nothing special; the
.hextension won’t change how the compiler handles them. It’s more of a hint for humans “this file probably doesn’t contain any code”.So if you put actual binary data in there, the compiler will create a copy of the array in each file in which you include the header (instead of simply adding a reference to a shared global array).
GIMP creates a header file because it doesn’t know how you plan to use the data. The idea is that you’ll include this header file exactly once in a
.cfile which then processes the data in some way. If it wrote a.cfile and you made changes to the code, GIMP would have to merge the changes when you ask it to update the data – it would be messy.