控制 Xcode 将包含的项目头文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2596695/
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
controlling which project header file Xcode will include
提问by Jean-Denis Muys
My Xcode project builds to variations of the same product using two targets. The difference between the two is only on which version of an included library is used. For the .c source files it's easy to assign the correct version to the correct target using the target check box. However, including the header file always includes the same one. This is correct for one target, but wrong for the other one.
我的 Xcode 项目使用两个目标构建为同一产品的变体。两者之间的区别仅在于使用了包含库的哪个版本。对于 .c 源文件,使用目标复选框很容易将正确的版本分配给正确的目标。但是,包含头文件总是包含相同的。这对一个目标是正确的,但对另一个目标是错误的。
Is there a way to control which header file is included by each target?
有没有办法控制每个目标包含哪个头文件?
Here is my project file hierarchy (which is replicated in Xcode):
这是我的项目文件层次结构(在 Xcode 中复制):
MyProject
TheirOldLib
theirLib.h
theirLib.cpp
TheirNewLib
theirLib.h
theirLib.cpp
myCode.cpp
and myCode.cpp does thing such as:
和 myCode.cpp 做这样的事情:
#include "theirLib.h"
…
somecode()
{
#if OLDVERSION
theirOldLibCall(…);
#else
theirNewLibCall(…);
#endif
}
And of course, I define OLDVERSION
for one target and not for the other.
当然,我OLDVERSION
为一个目标而不是另一个目标定义。
Note the #include
must be as shown. Both of the following fail with a file not found error:
注意#include
必须如图所示。以下两个都失败并出现文件未找到错误:
#include "TheirOldLib/theirLib.h"
#include "TheirNewLib/theirLib.h"
So is there a way to tell Xcode which theirLib.h
to include per target?
那么有没有办法告诉 XcodetheirLib.h
包含每个目标?
Constraints:
- the two header files have the same name. As a last resort, I could rename one of them, but I'd rather avoid that as this will lead to major hair pulling on the other platforms.
- having to change the #include
to add a reference to the enclosing folder is also something I'd rather avoid, because I would need to do it twice with a conditional compile directive.
- I'm free to tweak my project as I otherwise see fit
约束条件:
- 两个头文件具有相同的名称。作为最后的手段,我可以重命名其中一个,但我宁愿避免这样做,因为这会导致其他平台上的主要头发拉扯。
- 必须更改#include
以添加对封闭文件夹的引用也是我宁愿避免的事情,因为我需要使用条件编译指令执行两次。
- 我可以随意调整我的项目,因为我认为合适
Thanks for any help.
谢谢你的帮助。
回答by Jean-Denis Muys
The key part of the answer is to use USE_HEADERMAP = NO as suggested by Chris in a comment. Here are the details.
答案的关键部分是按照克里斯在评论中的建议使用 USE_HEADERMAP = NO 。这是详细信息。
Short recipe (checked in Xcode 3.2.2):
简短配方(在 Xcode 3.2.2 中检查):
add a custom build setting of USE_HEADERMAP = NO for each concerned target. Here is how:
1.1. Open the target's info panel on the "Build" pane.
1.2. Pull down the action pop-up menu at the bottom left of the window, select "Add User-Defined Setting".
1.3. In the newly added line, set the first column ("Setting") toUSE_HEADERMAP
, and the second column ("Value") toNO
.add the correct include path to each target (target Build settings "Header Search Paths"). In my example that would be:
2.1. addTheirOldLib
for "old" target
2.2. addTheirNewLib
for "new" target
为每个相关目标添加 USE_HEADERMAP = NO 的自定义构建设置。方法如下:
1.1。在“构建”窗格中打开目标的信息面板。
1.2. 下拉窗口左下方的操作弹出菜单,选择“添加用户定义的设置”。
1.3. 在新添加的行中,将第一列(“设置”)设置为USE_HEADERMAP
,将第二列(“值”)设置为NO
。为每个目标添加正确的包含路径(目标构建设置“标题搜索路径”)。在我的例子中,这将是:
2.1。添加TheirOldLib
“旧”目标
2.2。添加TheirNewLib
“新”目标
Step 1disables the automatic header map feature of Xcode, through which any header file included in the project is directly accessible through its name, whatever its actual path. When two headers have the same name, this feature leads to an unresolvable ambiguity.
步骤 1禁用 Xcode 的自动头文件映射功能,通过该功能可以通过名称直接访问项目中包含的任何头文件,无论其实际路径如何。当两个标题具有相同的名称时,此功能会导致无法解决的歧义。
Step 2allows for the #include "theirLib.h"
to work without qualifying the header file actual path name.
第 2 步允许在#include "theirLib.h"
不限定头文件实际路径名的情况下工作。
These two steps together fulfill my two constraints.
这两个步骤一起满足了我的两个约束。
Finally, USE_HEADERMAP
is notdocumented by Apple, as far as I can tell. I'll fill a bug report for that, as this setting is crucial in a number of cases, as googling for it reveals. Reported as rdar://7840694. Also on open Radar as http://openradar.appspot.com/radar?id=253401
最后,USE_HEADERMAP
是不是苹果记载,据我可以告诉。我将为此填写错误报告,因为此设置在许多情况下都至关重要,正如谷歌搜索所揭示的那样。报告为 rdar://7840694。同样在开放雷达上作为http://openradar.appspot.com/radar?id=253401
回答by bleater
USE_HEADERMAP=NO is overkill for some projects. It might be enough to just use HEADERMAP_INCLUDES_FLAT_ENTRIES_FOR_TARGET_BEING_BUILT=NO. Documentation here: https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html#//apple_ref/doc/uid/TP40003931-CH3-SW159
USE_HEADERMAP=NO 对于某些项目来说太过分了。仅使用 HEADERMAP_INCLUDES_FLAT_ENTRIES_FOR_TARGET_BEING_BUILT=NO 可能就足够了。此处的文档:https: //developer.apple.com/library/mac/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html#//apple_ref/doc/uid/TP40003931-CH3-SW159
回答by Azeem.Butt
Why can't you just use different include paths in each target?
为什么不能在每个目标中使用不同的包含路径?
回答by Ivan
Use USE_HEADERMAP=NO and in "User Header Search Paths" include your custom directory first and project directory recursively second: ${PROJECT_DIR}/TheirNewLib ${PROJECT_DIR}/**
使用 USE_HEADERMAP=NO 并在“用户标题搜索路径”中首先包含您的自定义目录,然后递归地包含项目目录:${PROJECT_DIR}/TheirNewLib ${PROJECT_DIR}/**