Version string?

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm working on an project, in one of the source code headers the sw version number is defined in two places, once as a set of defines and in another as a const char string.

I not sure how to have the compiler build the string at compile time so I only have to maintain the version in one place.

Currently I have this in the header:

    #define SW_VERSION_MAJOR        1
    #define SW_VERSION_MINOR        1
    #define SW_VERSION_MAINTENANCE  2
    ....
    const char var_firmware_rev[] = "1.1.1";
    
    

As you can see the two don't match, is there a way to build the var_firmware_rev[] string from the defined values above?

I'm not looking for a run time solution such as sprinf() as ram is tight on this system, prefer it to be built at compile time and saved in flash(const).

 

TIA

Jim

 

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
https://www.onegold.com/join/7134f67c2b814c5ca8144a458eccfd61

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The word is "stringify" ;-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Something like this should do it

#define SW_VERSION_MAJOR        1
#define SW_VERSION_MINOR        1
#define SW_VERSION_MAINTENANCE  2

#define STRINGIFY(x) STRINGIFY_(x)
#define STRINGIFY_(x) #x

const char var_firmware_rev[] = STRINGIFY(SW_VERSION_MAJOR) "."
                                STRINGIFY(SW_VERSION_MINOR) "."
                                STRINGIFY(SW_VERSION_MAINTENANCE);

You need the 2 stage STRINGIFY and STRINGIFY_  (or whatever you want to call them) function like macros.

This also uses string concatenation ie. "1"  "."  "1" is concatenated by compiler to "1.1"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Indeed. SO has this:

 

https://stackoverflow.com/questions/5459868/concatenate-int-to-string-using-c-preprocessor

 

That's pretty much identical to MrKendo's suggestion. The key thing in all that is the "#x" which is the preprocessor's "stringify" operation.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks guys!  Much appreciated smiley

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
https://www.onegold.com/join/7134f67c2b814c5ca8144a458eccfd61

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

PS a completely different approach for "version" is simply to build a string using the preprocessor __DATE__ and __TIME__ so it gets a version based on when you built it.

 

(I prefer this - it's too easy to forget to update major/minor/rev while the DATE and TIME auto update)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Going back to the version string, slightly different if you prefer (but exactly the same principle)

#define VERSION_STRING(major, minor, maintenance) VERSION_STRING_(major, minor, maintenance)
#define VERSION_STRING_(major, minor, maintenance) #major "." #minor "." #maintenance

const char rev[] = VERSION_STRING(SW_VERSION_MAJOR, SW_VERSION_MINOR, SW_VERSION_MAINTENANCE);

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'll argue for that in future projects, thanks for another great suggestion!

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
https://www.onegold.com/join/7134f67c2b814c5ca8144a458eccfd61

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

following, as this is something I also run in every now and then.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just to update the thread, I used the following code:

#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)


    #define SW_VERSION_MAJOR        1
    #define SW_VERSION_MINOR        1
    #define SW_VERSION_MAINTENANCE  2


const char var_firmware_rev[] = STR(SW_VERSION_MAJOR) "."
                                STR(SW_VERSION_MINOR) "."
                                STR(SW_VERSION_MAINTENANCE);
                                

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
https://www.onegold.com/join/7134f67c2b814c5ca8144a458eccfd61