Using C macros in eclipse post-build step

722 Views Asked by At

This question has been asked before (there are questions that are 5 or 10 years old) but without any real answer, usually a different approach has been used.

I'm working on a project where a different approach is simply not possible. We are using a third-party post-build step that needs some arguments (version) as part of the input. The version is set inside the C code using #define as some settings are set based on different parts of the version.

After some major changes, we have to recompile the code with different versions so I rather keep the version in a single location (in main.h preferably). Is there any way to do it in eclipse or do I have to bear the pain and just change it at multiple locations manually?

I'm using Eclipse Neon.3 Release (4.6.3), since I'm using system workbench and that's their default version.

2

There are 2 best solutions below

0
On BEST ANSWER

Using -D parameter (in project properties > C/C++ Build > settings > Tool Settings > Preprocessor) did not do the job for me as the macros or the build variables defined based on them were not expanding in the post-build step.

My workaround was to write a shell script to read the version from the header file and then pass it to the post-build. So I'm calling the other script inside my script that extracts the version. This way I can change the versions inside the code rather than the labyrinth of the eclipse settings.

This line extracts the version:

fw_version=$(cat "$projectdir/../Inc/main.h" | grep "FW_VERSION" | cut -d ' ' -f 3-)

1
On

You have some tool that does:

  1. Your build
  2. This post build step

Extract the version #define from your C project (in 1) and store it instead in a build system variable. Then pass it as a -D parameter to the necessary files (in 1), and as a parameter in whatever way it's expected by step 2.