xcode 如何将用户定义的宏传递给 xcodebuild?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11927601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to pass the user-defined macro to xcodebuild?
提问by qiushuitian
I build my project by xcodebuild in command line. Not in xCode. I want to pass some marc to the project so that it can affect the code. Such as code below:
我在命令行中通过 xcodebuild 构建我的项目。不在 xCode 中。我想将一些 marc 传递给项目,以便它可以影响代码。比如下面的代码:
#if (API_TYPE == 1)
#define URL_API @"https://dapi.xxx.com/1.1/"
#elif (API_TYPE == 2)
#define URL_API @"https://tapi.xxx.com/1.1/"
#elif (API_TYPE == 3)
#define URL_API @"https://api.xxx.com/1.1/"
#else
#error "API_TYPE value error! should be only value 1,2,3 !"
#endif
I want pass the define of API_TYPE outside the code files. Such as through xcodebuild command. But It doesn't work like this:
我想在代码文件之外传递 API_TYPE 的定义。比如通过 xcodebuild 命令。但它不是这样工作的:
xcodebuild -sdk xxx -target xxx SYMROOT=${XCSYMROOT} API_TYPE=${APITYPE}
The API_TYPE's value was not changed. But the SYMROOT's value was changed. So how can I implement that passing the value to API_TYPE outside ?
API_TYPE 的值未更改。但是 SYMROOT 的值被改变了。那么我怎样才能实现将值传递给 API_TYPE 外部呢?
回答by stonemonk
I know this is an old post, but why not just use:
我知道这是一篇旧帖子,但为什么不直接使用:
xcodebuild GCC_PREPROCESSOR_DEFINITIONS='$GCC_PREPROCESSOR_DEFINITIONS API_TYPE=1'
If you want to be able to have it optionally override an existing define in app, just code something like this:
如果您希望能够选择性地覆盖应用程序中的现有定义,只需编写如下代码:
#define DEFAULT_API_TYPE 1
#ifdef API_TYPE
#define REAL_API_TYPE API_TYPE
#else
#define REAL_API_TYPE DEFAULT_API_TYPE
#endif
回答by Eric Postpischil
The command-line setting you used effectively set an environment variable, at least within Xcode's build environment. But that build environment does not get passed into the compiler's preprocessor, just as setting a shell environment variable does not make that environment variable visible in the preprocessor.
您使用的命令行设置有效地设置了一个环境变量,至少在 Xcode 的构建环境中。但是该构建环境不会传递到编译器的预处理器中,就像设置 shell 环境变量不会使该环境变量在预处理器中可见一样。
To make it visible in the preprocessor, add an entry in the Preprocessor Macros build setting for the project, or for the specific targets you want. The entry should have the form “FOO=$(FOO)”. Xcode passes this to the compiler as “-DFOO=value of FOO from environment”.
要使其在预处理器中可见,请在预处理器宏构建设置中为项目或所需的特定目标添加一个条目。条目的格式应为“FOO=$(FOO)”。Xcode 将此作为“-DFOO=环境中 FOO 的值”传递给编译器。
When editing the Preprocessor Macros setting, be sure you are editing it for All Configurations (or the configuration you want to set it for, if you do not want to set it for all).
编辑预处理器宏设置时,请确保为所有配置(或要为其设置的配置,如果您不想为所有配置)编辑它。