How do I use the C preprocessor to make a substitution with an environment variable(如何使用 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 预处理器使用环境变量进行替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 C 预处理器使用环境变量进行替换
基础教程推荐
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
