how to pass the user-defined macro to xcodebuild?

妖精的绣舞 提交于 2019-12-01 19:45:19

问题


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:

#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:

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 ?


回答1:


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



回答2:


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.

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”.

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).



来源:https://stackoverflow.com/questions/11927601/how-to-pass-the-user-defined-macro-to-xcodebuild

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!