Define macro at compile time

One way to do this is using a cmake buildscript with mbed-cmake:

string(RANDOM LENGTH 8 ALPHABET "0123456789" IDENTIFIER_VAL)
target_compile_definitions(your_app __RANDOM__=${IDENTIFIER_VAL})

The length and content of the random string are configurable, the docs are here. Note that using the snippet above will regenerate the random value each time CMake is run, not every single build. If you need to regenerate the random value each build, then you can use a custom target that runs a script which generates a header file containing the random value, and then have your program include the header file. The custom target will get remade each time you run make, so the value will change each build.

2 Likes