jeudi 28 avril 2016

Appending a string literal with a compile time generated integer

I'm trying to generate a string during compile-time that contains a constant string and a couple calculated integers.

This string is to be used in GCC's attribute((section(""))) directive.

The purpose of the whole thing is to put a few variables in an ELF file, each with a unique section name.

I used to do this by compiling each object file with -DSOME_SYMBOL= to differentiate between object files, and COUNTER to differentiate between variables inside of a single object file. (We use this because of a requirement from our logging solution)

So the resulting code would be used using something like this:

#define SOME_MACRO(msg) {\
    static const char *messageBuffer __section__((section(".msg" ## #SOME_SYMBOL ## #__COUNTER__))) = {msg};\
} // Approximation

SOME_MACRO("This is a string");

This solution works great, but it requires support from the build-system (calculating the CRC and injecting it as a GCC -D flag), and it became a bit of an overhead when we moved from Makefile to SCons.

So I searched for another solution, and found this compile time CRC solution, but I got a bit lost when trying to figure out how to append it to the string.

After a bit more searching, I found the following answer, which explains how to convert an integer to a string using template metaprogramming, but I still couldn't figure out how to append the strings (again, during compile time).

I'd love to find a solution for this problem.

Aucun commentaire:

Enregistrer un commentaire