When using a logging framework it is useful to use the current file name or line number. The ANSI C/C++ standard defines the __LINE__ and __FILE_ preprocessor macros for this.
But what about the project name, if it is a release or debug build, the microcontroller used or other things like the operating system which was used to build the binary?
This (and even more) can be easily provided by Eclipse to the C/C++ application being built with CDT.
The trick is to know about the Eclipse CDT variables: they are available on a workspace level and on a project level. For example there is a CDT variable in the project settings available which has the project name stored:
Such variables can be used anywhere in the build process, e.g. with
${ProjName}
I can use them in the compiler settings and assign them to #defines set using the -D command line option like this to have them defined as strings:
__PROJECTNAME__=\"${ProjName}\" __TARGETCHIP__=\"${TargetChip}\" __CONFIGNAME__=\"${ConfigName}\"
Because the command line is passed through the host OS shell, double quotes might be removed, the other trick is to escape them with \” as shown above.
I can verify them passed on the command line in the Console view:
That works very well if the command line is passed fully to the compiler as shown above. However, if the build integration (as for example the NXP S32DS) passes the arguments in a ‘args’ file, then this does not work if the generator for that args file wrongly translates the escape sequence:
If this is the case: simply use normal double quotes for the -D:
If that does not help or is enough (check with the -E preprocess option), use the command line directly:
After that, I can used the #define as if they would have been set in the source code. For example I can store them in constants for later use, similar to the __FILE__ macro:
static const char fileName[] = __FILE__; static const char projectName[] = __PROJECTNAME__; static const char targetChipName[] = __TARGETCHIP__; static const char configName[] = __CONFIGNAME__;
The same way it works with any other Eclipse CDT variable.
Happy Variabling 🙂