I need a way to generate unique values using a preprocessor directive. The aim is that each time the macro is called, it will have a unique integral identifier. But it should retain it's value across files. Kind of like a preprocessor counter for the number of times a call is made to the function.
The macro I am using is:
#define LOG_MSG(a) log_msg(?)
This macro would be defined at a single place only. During the preprocessing stage, the '?' would be replaced by a unique identifier. We are doing this to reduce the overhead that comes with strings as this code will run on an embedded device. After the preprocessing the identifiers and the related strings would be extracted and a table would be created that would map them (this would be on the application side).
Since this will be used across multiple files, I wanted a way to generate a unique identifier (integral not string) for every use across the multiple files(an identifier for every unique string would be ideal but not necessary).
Any ideas?
Please do mention if there is any missing or incomplete information
Notes
__COUNTER__
was the first thing I tried, but it doesn't hold across files.__FILE__
would give me a string which defeats the purpose.
Some people mentioned using unique file identifiers. But I don't want to statically allocate these. We are using CCS(it's built on Eclipse Kepler) to build this code. So I was thinking that we could add something to the build system to do what @embedded_guy mentioned. Anyone know how to that?
Thanks
In general, it's not possible to assure a unique identifier is always generated for each macro expansion.
You can semi-solve the problem by using the builtin __LINE__
macro, assuming the macro is used at most once per line. However, since you need to use it across multiple files, you'll need to manually define another macro that is unique per file. For example, at the top of foo.c:
#define FILE_NAME foo
#include "log.h" // or whatever defines `LOG_MSG`
Then, you can use macro concatenation on FILE_NAME
and __LINE__
to generate a unique identifier.
If just a string will suffice (instead of an actual variable), you can use the __FILE__
macro as well, combined with the "stringized" version of __LINE__
like so:
#define FILE_LINE(x, y) FILE_LINE_(x, y)
#define FILE_LINE_(x, y) x ## #y
#define LOG_UNIQUE_ID_STRING FILE_LINE(__FILE__, __LINE__)
Again, this assumes at most one invocation per logical line.
Some compilers also support a __COUNTER__
macro, which can be used in place of __LINE__
, which allows for multiple invocations per logical line (but again, you need to combine it with something else, since __COUNTER__
is not unique across multiple translation units (aka C files)).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With