XCode 5.1 预处理器宏不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22359524/
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
XCode 5.1 preprocessor macro not working
提问by Chuck Krutsinger
I cannot get this macro to compile the correct code.
我无法让这个宏编译正确的代码。
Here is the code:
这是代码:
Here are the build settings (I'm doing a Release build):
Note that the GCC documentation says -Dname will define as 1, so I omitted the "=1" for Release:
以下是构建设置(我正在进行发布构建):
请注意,GCC 文档说 -Dname 将定义为 1,因此我省略了发布的“=1”:
Here is the compile log showing that the definition (in yellow) was passed along on the command line:
这是编译日志,显示定义(黄色)是在命令行上传递的:
Here is my output log showing that the code is compiled as if ADD_CAMERA_FEATURE is not defined:
这是我的输出日志,显示代码被编译为好像没有定义 ADD_CAMERA_FEATURE:
If I put #define ADD_CAMERA_FEATURE 1 in the source, the #ifdef works as expected, but I also get a warning that I am redefining an existing macro. So XCode knows that the macro should exist from the build scheme settings, but still does not include the #ifdef branch of code.
如果我将 #define ADD_CAMERA_FEATURE 1 放在源代码中,#ifdef 会按预期工作,但我也会收到警告,提示我正在重新定义现有的宏。因此 XCode 知道该宏应该存在于构建方案设置中,但仍然不包括代码的 #ifdef 分支。
Other details:
其他详情:
- XCode 5.1
- OS X 10.9.2
- iOS 7.1
- 代码 5.1
- OS X 10.9.2
- iOS 7.1
My objective here is to have a target for building iOS 7 version of the app and a target for building a pre-iOS 7 version of the app, both from the same source. I have to support older devices that cannot be upgraded to iOS 7 for a while longer. Perhaps there is a better way to go about this. Any suggestions about how to accomplish this would be appreciated.
我的目标是建立一个用于构建 iOS 7 版本应用程序的目标和一个用于构建 iOS 7 之前版本应用程序的目标,两者都来自同一来源。我必须支持一段时间内无法升级到 iOS 7 的旧设备。也许有更好的方法来解决这个问题。任何有关如何实现这一点的建议将不胜感激。
采纳答案by Chuck Krutsinger
Found the problem. It has to do with targets and dependencies. I created a new target to compile the source file and added the preprocessor definition to that target. That compiled object then got linked into a static library being used as a framework. So I also created a new target for the static library. Unfortunately, I overlooked that the static library target was still depending on the original compile step that did not include the preprocessor definition. As a result, even though I was building the object file correctly, the new object file was not the one being linked into the project at runtime. So under Build Phases for the static library, I needed to change the target dependency to the correct object file and everything began to work. Thanks @matt and @StevenFisher for pointing me toward the right settings.
发现问题了。它与目标和依赖关系有关。我创建了一个新目标来编译源文件并将预处理器定义添加到该目标。然后,该编译对象链接到用作框架的静态库中。所以我还为静态库创建了一个新目标。不幸的是,我忽略了静态库目标仍然依赖于不包含预处理器定义的原始编译步骤。结果,即使我正确地构建了目标文件,新的目标文件也不是在运行时链接到项目的那个。因此,在静态库的构建阶段下,我需要将目标依赖项更改为正确的对象文件,然后一切开始工作。感谢 @matt 和 @StevenFisher 为我指出正确的设置。