How to use Eclipse CDT Environment Variables in C/C++ Code

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?

Target Chip Name

Target Chip Name

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:

Available CDT Build Variables for Project

Available CDT Build Variables for Project

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}\"
Defined Symbols with Eclipse CDT Variables

Defined Symbols with Eclipse CDT Variables

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:

Defines passed on the command line

Defines passed on the command line

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:

wrong args file

wrong args file

If this is the case: simply use normal double quotes for the -D:

normal double quotes

normal double quotes

If that does not help or is enough (check with the -E preprocess option), use the command line directly:

-D option in command line

-D option in command line

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 🙂

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.