在C ++中使用环境变量作为编译时常数 [英] Use environment variable as compile time constant in C++
问题描述
例如,我已经定义了一个名为 BUILD_VERSION
的环境变量,并将其设置为 1.0.0
,当我编译时 1.0.0
被烘烤成我的代码。 EG:
批处理文件:
设置BUILD_VERSION = 1.0.0
;调用vs编译器
C ++文件:
const std :: string build_version = BUILD_VERSION //这将导致1.0.0。
我该怎么做?
最后,我按照 txchelp 的建议,并添加了一个 / D
标记到项目属性的命令行 - >其他选项部分,以将环境变量声明为预处理器定义。
看起来像这样:
然后在启动构建的批处理脚本中:
set SVN_BUILD_VERSION = 1.0.0
最后将其解压缩为字符串在源代码中:
#define STRINGIZER(arg)#arg
#define STR_VALUE(arg)STRINGIZER(arg )
#define BUILD_VERSION_STRING STR_VALUE(BUILD_VERSION)
// ...
const std :: string version = BUILD_VERSION_STRING; //结果为1.0.0。
As part of a build process, I need to take an environment variable defined by a batch script and use it as a constant within the code at compile time.
For example, say I have defined an environment variable named BUILD_VERSION
and set it to 1.0.0
, when compiled I want 1.0.0
to be baked into my code. EG:
Batch file:
set BUILD_VERSION = 1.0.0
; call vs compiler
C++ File:
const std::string build_version = BUILD_VERSION // Which will result in "1.0.0".
How would I go about doing this?
In the end I followed txchelp advice and added a /D
flag into the Command Line -> Additional Options section of the project properties to declare the environment variable as a preprocessor definition.
It looked something like this:
Then in the batch script that started the build:
set SVN_BUILD_VERSION=1.0.0
And finally to extract it as a string within the source code:
#define STRINGIZER(arg) #arg
#define STR_VALUE(arg) STRINGIZER(arg)
#define BUILD_VERSION_STRING STR_VALUE(BUILD_VERSION)
// ...
const std::string version = BUILD_VERSION_STRING; // Results in "1.0.0".
这篇关于在C ++中使用环境变量作为编译时常数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!