How to put a string in a define in build flag into a libary.json file

Hi everyone :slight_smile:

I would like to know if it’s possible to put a string in a define (ex : #define BONJOUR “Bonjour”)into build flag in a library.json file.

Here is my build flag in my library.json file :
“build” : {
“flags”: [
“-I Inc”,
“-I OD”,
“-DVERSION=1.2”
],
“extraScript”: “select_board_script.py”
},

I want to do something like this -DVERSION = “1.2” but there are already quotes and I can’t do “-DVERSION =” 1.2 “”.

I try to use escape characters by doing this:
"-DVERSION = " 1.2 \ “”
But it does not work

How can I do this?

Thanks in advance for your time.

All defines are of type string. There is no need for additional quotes:

"flags": [
    "-DVERSION=1.2",
    "-DBONJOUR=Bonjour"
],
1 Like

Hi,

here "-DVERSION=1.2" will be usable as float, for example by using in the code:

float revision = VERSION

so to make it string we can use conventional float to string tools.

But how to get a library revision such as 0.10.6 from the library.json into the code?

There is multiple problems here :

  1. how to avoid copy paste the revision number on the library.json into a -DVERSION=*.*.* using a version variable or something?
  2. how to pass the value in a define as a string? Indeed -DVERSION=0.10.6 for example cause a compilation error because it’s considered as a float with too many ‘.’ in it. I also try -DVERSION=v0.10.6 but it fails too. We have to find a way to specify it as a string and I don’t get any way for now.

Have you tried it with explicit string escaping regardless and save it into a const char []? Like "-DVERSION=\"v0.10.6\""

I don’t have any good idea how you can avoid having the revision number twice.

But the other part can be solved. Assuming you have a build flag like:

-DVERSION=1.2.3

Then you can turn it into a string constant like so:

#define STRINGIFY(s) STRINGIFY1(s)
#define STRINGIFY1(s) #s

const char* version = STRINGIFY(VERSION);

Don’t ask why. It has to do with the inner workings of the preprocessor.

3 Likes

Yes I tried but the '' doesn’t seems to work as i expected.

I never heard of this tricks! I’m gonna try it and let you know.

Thank’s a lot @manuelbl your STRINGIFY trick work perfectly!

Just to dig it a lot more for potential readers (and because I struggle to figure it out to find a solution).

I also need to access the lib dependance revision.
To make it clear, here is my depancy graph :

Dependency Graph
|-- <Luos> 0.6.6
|   |-- <Robus> 1.1.0

Luos lib have to be able to get Luos and Robus revision. For Luos revision the STRINGIFY thing make it. But you can only get the defined version from the library.json into .c files of the lib and only .c of the lib.
This is because platformio compile lib as library (**.a), so the -DVERISON=x.x.x is only available into .c and not into .h files of the lib. #defined value are not variables, so after the library compilation the #DEFINE is not available anymore to other code parts.
To make it work I had to put the define into memory in a .c.
Something like :

const char VERSION_ROBUS[10] = VERSION; // which is x.x.x

and put it as externe in a .h making it available for other libs :

const extern char VERSION_ROBUS[10];
2 Likes