Include line in file at compile time

I am working on a team project using teensy and matlab, and to avoid version differences (for example one person downloads teensy with version A, and the person who is now using it with matlab has version B of the code) I would like to post the version line when mating.

However, I want the version string to be in a shared file between the matlab and teensy code, and every time the program is loaded into teensy, include it in the compilation as a constant.

View like:

const string version = "<included file content>";

      

Matlab for its part can read it at runtime.

I was thinking about using a file whose content is assignment to a variable whose name is shared by both teensy and matlab, however I would prefer a more elegant solution if such exists, especially one that does not involve executing code from an external file at runtime ...

+3


source to share


1 answer


One way is to simply set the same setting:

version.inc:

"1.0.0rc1";

      

main.cpp:

const string version = 
#include "version.inc"

...

      

Note that the newline between =

and #include

is in place to keep the compiler happy. Also, if you don't want to include the semicolon in the file.inc



, You can do it:

main.cpp:

const string version = 
#include "version.inc"
; // Put the semicolon on a newline, again to keep the compiler happy

      


EDIT: instead of a file .inc



you can indeed have any file extension you desire. It's all down to taste


EDIT: If you really want to, you can omit the quotes from the file .inc



but this will lead to confusing code:

version.inc:

STRINGIFY(
    1.0.0rc1
);

      

main.cpp:

#define STRINGIFY(X) #X

const string version = 
#include "version.inc"
...

      


EDIT:

As @ Ôrel pointed out, you can handle generation version.h

or similar in your Makefile. Assuming you are using a * nix system, you can try a setup like this:

Makefile:

...
# "1.0.0rc1"; > version.h
echo \"`cat version.inc`\"\; > version.h
...

      

version.inc:

1.0.0rc1

      

main.cpp:

const string version = 
#include "version.h"

      

+4


source







All Articles