C++ 编译时在文件中包含字符串
我使用a和matlab处理团队项目,为了避免版本差异(例如,一个人使用版本a加载teensy,而现在使用matlab的人拥有代码的版本B),我希望在配对时发送一个版本字符串 但是,我希望版本字符串位于matlab代码和teensy之间的共享文件中,并且每次将程序加载到teensy时,都将其作为常量包含在编译中 有点像:C++ 编译时在文件中包含字符串,c++,c,compilation,arduino,C++,C,Compilation,Arduino,我使用a和matlab处理团队项目,为了避免版本差异(例如,一个人使用版本a加载teensy,而现在使用matlab的人拥有代码的版本B),我希望在配对时发送一个版本字符串 但是,我希望版本字符串位于matlab代码和teensy之间的共享文件中,并且每次将程序加载到teensy时,都将其作为常量包含在编译中 有点像: const string version = "<included file content>"; const string version=”“; matlab
const string version = "<included file content>";
const string version=”“;
matlab本身可以在运行时读取它
我曾想过使用一个文件,其内容是对一个变量的赋值,该变量的名称由teensy和matlab共享,但是如果存在这样的解决方案,我更喜欢一个更优雅的解决方案,尤其是一个不包括在运行时从外部文件执行代码的解决方案。一种方法就是进行如下简单设置: version.inc:
"1.0.0rc1";
STRINGIFY(
1.0.0rc1
);
1.0.0rc1
main.cpp:
const string version =
#include "version.inc"
...
const string version =
#include "version.inc"
; // Put the semicolon on a newline, again to keep the compiler happy
#define STRINGIFY(X) #X
const string version =
#include "version.inc"
...
const string version =
#include "version.h"
请注意,=
和#include
之间的换行符是为了让编译器满意。此外,如果不想在.inc
文件中包含分号,可以执行以下操作:
main.cpp:
const string version =
#include "version.inc"
...
const string version =
#include "version.inc"
; // Put the semicolon on a newline, again to keep the compiler happy
#define STRINGIFY(X) #X
const string version =
#include "version.inc"
...
const string version =
#include "version.h"
编辑:您可以拥有所需的任何文件扩展名,而不是
.inc
文件。这完全取决于口味
编辑:如果确实需要,可以省略
.inc
文件中的引号,但这样会导致代码混乱,如下所示:
version.inc:
"1.0.0rc1";
STRINGIFY(
1.0.0rc1
);
1.0.0rc1
main.cpp:
const string version =
#include "version.inc"
...
const string version =
#include "version.inc"
; // Put the semicolon on a newline, again to keep the compiler happy
#define STRINGIFY(X) #X
const string version =
#include "version.inc"
...
const string version =
#include "version.h"
编辑: 正如@Ôrel所指出的,您可以在Makefile中处理
version.h
或类似版本的生成。假设您正在运行*nix系统,您可以尝试如下设置:
生成文件:
...
# "1.0.0rc1"; > version.h
echo \"`cat version.inc`\"\; > version.h
...
version.inc:
"1.0.0rc1";
STRINGIFY(
1.0.0rc1
);
1.0.0rc1
main.cpp:
const string version =
#include "version.inc"
...
const string version =
#include "version.inc"
; // Put the semicolon on a newline, again to keep the compiler happy
#define STRINGIFY(X) #X
const string version =
#include "version.inc"
...
const string version =
#include "version.h"
只是为了澄清,在<代码>“<代码> >中,你在寻找文件名或内容吗?内容,它是代码版本的一个字符串,比如“1.0.0RC1”,你可以考虑Dblahblahgcc@WojciechFrohmbergOP请求将输入作为编译常量:“
。。。每次将程序加载到teensy时,将其作为常量包含在编译中
似乎你只读了问题的一部分。OP要求在运行时将文件包含在matlab端我考虑过这一点,有没有办法让它不带引号?@Levi就是。。令人担忧,因为我认为我在生产代码中使用了类似的东西。谢谢!您可以通过Makefile进行操作,从version.inc
生成version.h
,或者更好的选择,以避免错误的操作。您可以使用提交id生成版本。这看起来非常好,尽管我不确定如何在arduino IDE中使用makefiles,但我会检查它!