如何使用C预处理器来替换环境变量 [英] How do I use the C preprocessor to make a substitution with an environment variable

查看:141
本文介绍了如何使用C预处理器来替换环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我希望 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"

即使在运行时将 MY_VERSION 设置为其他任何东西,pluginRequires 将被设置为2010.4。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆