3

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.

nadavge
  • 590
  • 1
  • 3
  • 14

1 Answers1

4

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"
Levi
  • 1,921
  • 1
  • 14
  • 18
  • 1
    @WojciechFrohmberg The OP asked for the input as a compilation constant: "`... and every time the program is loaded to the teensy, have it included on compilation as a constant`". It seems as though you have read only part of the question. The OP asks for the file to be included at runtime on the matlab side – Levi Jun 01 '15 at 10:36
  • I considered this, is there a way to have it without the quotation marks? – nadavge Jun 01 '15 at 10:41
  • @nadavge Unfortunately, I believe this is beyond the capabilities of the C preprocessor. – Levi Jun 01 '15 at 10:43
  • @nadavge It may be possible, using the `#` preprocessor operator. – mtijanic Jun 01 '15 at 10:46
  • @mtijanic: That's undefined behaviour. ([See here](http://stackoverflow.com/a/6502494/2636457)) – Levi Jun 01 '15 at 10:50
  • 1
    @Levi That is.. worrying, since I think I've used something similar in production code. Thanks a lot! – mtijanic Jun 01 '15 at 10:53
  • 1
    You can do the manipulation via your Makefile, generate `version.h` from `version.inc` or better choice to avoid bad manipulation you can use the commit id to generate the version. – Ôrel Jun 01 '15 at 10:54
  • @Ôrel: Good point about the generation of `version.h`. @nadavge: See updated answer – Levi Jun 01 '15 at 11:08
  • 1
    It seems very good, even though I'm not sure how to use makefiles in the arduino IDE, I'll check it out! – nadavge Jun 01 '15 at 11:24
  • This doesn't work. The teensy code sits in code/teensy and the matlab code at code/matlab. I wish the version to sit in code/version. When I try to include: "../version" from the teensy, it says the file is not found. Even when the file sits in the same directory as the teensy code, unless the ending is .h it doesn't find the file – nadavge Jun 02 '15 at 22:07
  • @nadavge The problem may be that you are building source files which require `version.h` before `version.h` is generated. Also, when you say you're trying to include "../version", do you mean you are trying to do `#include "../version"` or `#include "../version/version.h"`? The former will not work as you cannot `#include` directories in C/C++ – Levi Jun 03 '15 at 04:39