如何使用C预处理器用环境变量替换 [英] How do I use the C preprocessor to make a substitution with an environment variable
问题描述
在下面的代码中,我希望 THE_VERSION_STRING
的值取自环境变量 MY_VERSION
在编译时
In the code below, I would like the value of THE_VERSION_STRING
to be taken from the value of the environment variable MY_VERSION
at compile time
namespace myPluginStrings {
const char* pluginVendor = "me";
const char* pluginRequires = THE_VERSION_STRING;
};
因此,如果我输入:
export MY_VERSION="2010.4"
pluginRequires
将被设置为2010.4,即使 MY_VERSION
在运行时设置为其他值。
pluginRequires
will be set at "2010.4", even if MY_VERSION
is set to something else at run time.
更新:(feb 21)感谢您的帮助大家。有用。
由于我使用Rake作为构建系统,我的每个CFLAGS是一个ruby变量。此外,值需要以引号括起来。因此,gcc命令行需要如下:
UPDATE: (feb 21) Thanks for your help everyone. It works. As I'm using Rake as a build system, each of my CFLAGS is a ruby variable. Also the values need to end up in quotes. Therefore the gcc command line for me needs to look like this:
gcc file.c -o file -D"PLUGIN_VERSION=\"6.5\""
这意味着这是在我的Rakefile:
Which means this is in my Rakefile:
"-D\"PLUGIN_VERSION=\\\"#{ENV['MY_VERSION']}\\\"\""
推荐答案
如果我记得正确,命令行参数 -D
与gcc到 #define
在编译时的值。
If I recall correctly, you can use the command line parameter -D
with gcc to #define
a value at compile time.
ie:
$ gcc file.c -o file -D"THE_VERSION_STRING=${THE_VERSION_STRING}"
这篇关于如何使用C预处理器用环境变量替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!