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

查看:40
本文介绍了如何使用 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"

pluginRequires 将设置为2010.4",即使 MY_VERSION 在运行时设置为其他值.

pluginRequires will be set at "2010.4", even if MY_VERSION is set to something else at run time.

更新:(2 月 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.

即:

$ gcc file.c -o file -D"THE_VERSION_STRING=${THE_VERSION_STRING}"

这篇关于如何使用 C 预处理器使用环境变量进行替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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