Search code examples
cstatic-libraries

selective variable definition in a static C library


Im making a C static library file (.lib) for a Graphical LCD based on 8051 Microcontroller. there are some functions and 3 fonts which can be selected before compile(only one font is allowed). the library should contain some different fonts like :

#if defined FONT1
const uint8_t code font[size1] = { /* Font1 Data*/ }
#elif defined FONT2
const uint8_t code font[size2] = { /* Font2 Data*/ }
…

and the end user may select one desired font by defining it in the given ".h" file. when I tested, it was not possible to do this by writing above code as the library file, just compiles with one defined font which was obvious. on other hand, it's not efficient to load all 3 fonts at once (in the end application) because of memory considerations. is there a proper way to do so ?


Solution

  • You could possibly split the fonts up in different source files. Each source file would then be built into a separate object file (translation unit) and if your linker is good enough it will discard object files containing symbols not used.

    If the unused object files are discarded then they code and data in them will of course not be part of the final linked executable.