Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

c++ - How do I use the C preprocessor to make a substitution with an environment variable

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;
};

So that if I type:

export MY_VERSION="2010.4"

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

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"" 

Which means this is in my Rakefile:

"-D"PLUGIN_VERSION="#{ENV['MY_VERSION']}"""
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I recall correctly, you can use the command line parameter -D with gcc to #define a value at compile time.

i.e.:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...