简体   繁体   English

在编译时在文件中包含字符串

[英]Include string in file on compilation

I work on a team project using a teensy and matlab, and to avoid version differences (eg 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. 我使用teensy和matlab进行团队项目,并避免版本差异(例如,一个人加载版本A的teensy,现在使用matlab的人有代码的版本B),我想发送配对上的版本字符串。

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. 但是,我希望版本字符串位于matlab代码和teensy之间的共享文件中,并且每次将程序加载到teensy时,都将其作为常量包含在编译中。

Sort of like: 有点像:

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

The matlab on its part can read it at runtime. matlab本身可以在运行时读取它。

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. 我想过使用一个文件,其内容是一个名称由teensy和matlab共享的变量的赋值,但是如果存在这样的话,我宁愿选择一个更优雅的解决方案,特别是那个不包括从外部文件执行代码的解决方案。运行。

One way is just to have a simple setup like so: 一种方法是只需要一个简单的设置:

version.inc: version.inc:

"1.0.0rc1";

main.cpp: 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. 请注意, =#include之间的换行符是为了让编译器满意。 Also, if you don't want to include the semicolon in the .inc file, you can do this: 此外,如果您不想在.inc文件中包含分号,则可以执行以下操作:

main.cpp: 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. 编辑:您可以真正拥有所需的任何文件扩展名,而不是.inc文件。 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: 编辑:如果你真的想,你可以省略.inc文件中的引号,但这会导致像这样的乱码:

version.inc: version.inc:

 STRINGIFY( 1.0.0rc1 ); 

main.cpp: 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. 正如@Ôrel指出的那样,你可以在Makefile中处理version.h或类似的代码。 Assuming you're running a *nix system, you could try a setup like this: 假设您正在运行* nix系统,您可以尝试这样的设置:

Makefile: Makefile文件:

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

version.inc: version.inc:

 1.0.0rc1 

main.cpp: main.cpp中:

 const string version = #include "version.h" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM