控制Xcode将包含哪个项目头文件 [英] controlling which project header file Xcode will include

查看:131
本文介绍了控制Xcode将包含哪个项目头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Xcode项目使用两个目标构建到同一产品的变体。两者之间的区别仅在于使用了哪个版本的库。对于.c源文件,使用目标复选框可以很容易地将正确的版本分配给正确的目标。但是,包括头文件始终包含相同的头文件。这对于一个目标是正确的,但对另一个目标是错误的。

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.

有没有办法控制每个目标包含哪个头文件?

Is there a way to control which header file is included by each target?

这是我的项目文件层次结构(在Xcode中复制):

Here is my project file hierarchy (which is replicated in Xcode):

MyProject
  TheirOldLib
    theirLib.h
    theirLib.cpp
  TheirNewLib
    theirLib.h
    theirLib.cpp
myCode.cpp

和myCode.cpp做的事情如下:

and myCode.cpp does thing such as:

#include "theirLib.h"
…
somecode()
{
#if OLDVERSION
  theirOldLibCall(…);
#else
  theirNewLibCall(…);
#endif
}

当然,我定义 OLDVERSION 用于一个目标而不用于另一个目标。

And of course, I define OLDVERSION for one target and not for the other.

注意 #include 必须如图所示。以下两个都失败,找不到文件错误:

Note the #include must be as shown. Both of the following fail with a file not found error:

#include "TheirOldLib/theirLib.h"
#include "TheirNewLib/theirLib.h"

那么有没有办法告诉Xcode theirLib.h 包含每个目标?

So is there a way to tell Xcode which theirLib.h to include per target?

约束:

- 这两个头文件名相同。作为最后的手段,我可​​以重命名其中一个,但我宁愿避免这样做,因为这将导致其他平台上的主要头发。

- 必须更改 #include 添加对封闭文件夹的引用也是我宁愿避免的,因为我需要使用条件编译指令执行两次。

- 我'我可以自由调整我的项目,因为我认为合适

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

感谢您的帮助。

推荐答案

答案的关键部分是在评论中使用Chris建议的USE_HEADERMAP = NO。以下是详细信息。

The key part of the answer is to use USE_HEADERMAP = NO as suggested by Chris in a comment. Here are the details.

短配方(在Xcode 3.2.2中检查):

Short recipe (checked in Xcode 3.2.2):


  1. 为每个相关目标添加USE_HEADERMAP = NO的自定义构建设置。方法如下:

    1.1。在构建窗格中打开目标的信息面板。

    1.2。下拉窗口左下角的操作弹出菜单,选择添加用户自定义设置。

    1.3。在新添加的行中,将第一列(设置)设置为 USE_HEADERMAP ,将第二列(值)设置为

  1. 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") to USE_HEADERMAP, and the second column ("Value") to NO.

为每个目标添加正确的包含路径(目标构建设置标题搜索路径)。在我的例子中,这将是:

2.1。为旧目标添加 TheirOldLib

2.2。为新目标添加 TheirNewLib

add the correct include path to each target (target Build settings "Header Search Paths"). In my example that would be:
2.1. add TheirOldLib for "old" target
2.2. add TheirNewLib for "new" target

步骤1 禁用Xcode的自动标题映射功能,通过该功能可以通过其名称直接访问项目中包含的任何头文件,无论其实际路径如何。当两个标题具有相同的名称时,此功能会导致无法解决的歧义。

Step 1 disables 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.

第2步允许#包括theirLib.h在没有限定头文件实际路径名的情况下工作。

Step 2 allows for the #include "theirLib.h" to work without qualifying the header file actual path name.

这两个步骤一起完成了我的两个约束。

These two steps together fulfill my two constraints.

最后,据我所知, USE_HEADERMAP 由Apple记录。我将填写一份错误报告,因为这个设置在许多情况下都是至关重要的,因为谷歌搜索显示。报告为rdar:// 7840694。同样在开放的雷达上 http://openradar.appspot.com/radar?id=253401

Finally, USE_HEADERMAP is not documented 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

这篇关于控制Xcode将包含哪个项目头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆