0

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.

anishtain4
  • 2,342
  • 2
  • 17
  • 21
  • 1
    Some [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) would be helpful – Jorengarenar Jul 15 '20 at 22:15
  • do not do it in the .h file. Define the symbol in the eclipse and pass it as -D parameter to the gcc. – 0___________ Jul 15 '20 at 22:34
  • @P__J__ Is there a reason why I shouldn't put it in the .h file or is it just easier to put it in the symbols? Because the symbols of release and debug are different, putting it there means I should change it in two places. – anishtain4 Jul 15 '20 at 22:56
  • The advantage of including the version information in a header is that you can version control the information. – Jonathan Leffler Jul 16 '20 at 03:51

2 Answers2

0

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.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • Can you elaborate a little more? How to extract it from my C code and I'm not sure how to pass it as a `-D` parameter. – anishtain4 Jul 15 '20 at 22:55
0

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-)

anishtain4
  • 2,342
  • 2
  • 17
  • 21