Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include string in file on compilation

I work on a team project using a teensy and matlab, and to avoid version differences (e.g one person loads the teensy with version A, and the person now using it with matlab has version B of the code), I'd like to send a version string on pairing.

However, I want the version string to sit in a shared file between the matlab code and the teensy, and every time the program is loaded to the teensy, have it included on compilation as a constant.

Sort of like:

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

The matlab on its part can read it at runtime.

I thought of using a file whose contents are an assignment to a variable whose name is shared both by teensy and matlab, however I would prefer a more elegant solution if such exists, especially one that doesn't include executing code from an external file at runtime.

like image 650
nadavge Avatar asked May 23 '26 20:05

nadavge


1 Answers

One way is just to have a simple setup like so:

version.inc:

"1.0.0rc1";

main.cpp:

const string version = 
#include "version.inc"

...

Note that the newline between the = and the #include is in place to keep the compiler happy. Also, if you don't want to include the semicolon in the .inc file, you can do this:

main.cpp:

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


EDIT: Instead of a .inc file, you can really have any file extension you desire. It's all up to taste


EDIT: If you really wanted to, you could omit the quotes from the .inc file, but that would lead to messy code like this:

version.inc:

STRINGIFY(
    1.0.0rc1
);

main.cpp:

#define STRINGIFY(X) #X

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


EDIT:

As @Ôrel pointed out, you could handle the generation of a version.h or similar in your Makefile. Assuming you're running a *nix system, you could 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"
like image 145
Levi Avatar answered May 25 '26 11:05

Levi