为什么Xcode 6在系统路径之前错误地包含用户路径?

时间:2021-09-11 22:52:10

I'm trying to build a project I have done for Windows, Visual Studio under OSX using Xcode 6.1.1.

我正在尝试使用Xcode 6.1.1在OSX下构建我为Windows,Visual Studio完成的项目。

I'm running into an issue where in a file that needs to include #include <string.h>. However, in the same folder as the file that does this there is also a file that is named string.h.

我遇到了一个需要包含#include 的文件。但是,在与执行此操作的文件相同的文件夹中,还有一个名为string.h的文件。

Under my Visual Studio project this still resolves file, the system paths are searched first.

在我的Visual Studio项目下,这仍然解析文件,首先搜索系统路径。

In the Xcode project I have made sure to set up my own paths under "User Header Search Paths" - Xcode expand to the correct path. I also set "Always Search User Paths" to No - which according to the docs says that the system paths should be searched first: https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html#//apple_ref/doc/uid/TP40003931-CH3-SW110

在Xcode项目中,我确保在“用户标题搜索路径”下设置自己的路径--Xcode扩展到正确的路径。我还将“始终搜索用户路径”设置为“否” - 根据文档说明应首先搜索系统路径:https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1 -Build_Setting_Reference / build_setting_ref.html#// apple_ref / DOC / UID / TP40003931-CH3-SW110

But yet the #include <string.h> seem to be treated as #include "string.h" for some reason.

但是由于某种原因,#include 似乎被视为#include“string.h”。

The config is defined in the project and I've made sure the targets doesn't override this.

配置是在项目中定义的,我确保目标不会覆盖它。

Is this some Xcode/OSX thing where system includes <> search the path of the including file first?

这是系统包含<>首先搜索包含文件的路径的一些Xcode / OSX吗?

为什么Xcode 6在系统路径之前错误地包含用户路径?

My local string.h file is located at ruby/api/string.h relative to the include path in my User Header Search Path.

我的本地string.h文件位于ruby / api / string.h,相对于我的用户标题搜索路径中的包含路径。


Results of https://gist.github.com/thomthom/034b539bede38dd68261: https://gist.github.com/thomthom/034b539bede38dd68261

https://gist.github.com/thomthom/034b539bede38dd68261的结果:https://gist.github.com/thomthom/034b539bede38dd68261

2 个解决方案

#1


2  

The paths searched to satisfy an #include directive and the order in which they are searched are implementation defined. That includes whether any user-specified include paths (if even supported) are searched before, after, or instead of any default paths.

搜索到满足#include指令的路径以及搜索它们的顺序是实现定义的。这包括是否在任何默认路径之前,之后或代替任何默认路径搜索任何用户指定的包含路径(如果支持)。

That's the case for both forms of #include directive. In particular, although it is common for implementations to perform some kind of relative search to find a file specified using the double-quoted include syntax, C does not require that. It requires only that if the implementation-defined mechanism used for resolving double-quoted includes fails, the compiler must fall back on the implementation-defined method used for resolving angle-bracketed includes.

两种形式的#include指令就是这种情况。特别是,尽管实现通常执行某种相对搜索以查找使用双引号包含语法指定的文件,但C不需要这样。它只需要,如果用于解析双引号包含的实现定义机制失败,编译器必须依赖于用于解析包含角度括号的包含的实现定义方法。

Moreover, C specifies behavior only for the case that the given header name uniquely identifies a file to include. Depending on how one construes "uniquely", one might claim that C does not define any behavior at all in the situation you describe, on the basis that you have not uniquely identified the header you want to include. That's a bit wild, though -- I think it's better to interpret "uniquely" in light of the implementation-defined method for locating headers.

此外,C仅针对给定标头名称唯一标识要包括的文件的情况指定行为。根据一个构造“唯一”的方式,可能会声称C在您描述的情况下根本没有定义任何行为,因为您没有唯一标识要包含的标头。虽然这有点疯狂 - 我认为根据实现定义的定位头的方法来解释“唯一”更好。

Your best bet is to avoid header names that collide with standard header names. One version of that would be to put them in a subdirectory that will lend its name as a prefix. For example, put string.h in src/myapp/, and include it as

最好的办法是避免与标准标题名称冲突的标题名称。其中一个版本是将它们放在一个子目录中,该子目录将其名称作为前缀。例如,将string.h放在src / myapp /中,并将其包含为

#include "myapp/string.h"

To make that as safe as possible, ensure that directory src/ is in the include search path.

要使其尽可能安全,请确保目录src /位于包含搜索路径中。

#2


1  

Upon viewing your gist I noticed a discrepancy from your screenshot:

在查看您的要点后,我发现您的屏幕截图存在差异:

HEADER_SEARCH_PATHS = /Users/thomas/SourceTree/SUbD/SUbD/../ThirdParty/include/ruby/mac /Users/thomas/SourceTree/SUbD/SUbD/../ThirdParty/include/ruby/mac/universal-darwin12.5.0

This should be:

这应该是:

HEADER_SEARCH_PATHS = /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include

#1


2  

The paths searched to satisfy an #include directive and the order in which they are searched are implementation defined. That includes whether any user-specified include paths (if even supported) are searched before, after, or instead of any default paths.

搜索到满足#include指令的路径以及搜索它们的顺序是实现定义的。这包括是否在任何默认路径之前,之后或代替任何默认路径搜索任何用户指定的包含路径(如果支持)。

That's the case for both forms of #include directive. In particular, although it is common for implementations to perform some kind of relative search to find a file specified using the double-quoted include syntax, C does not require that. It requires only that if the implementation-defined mechanism used for resolving double-quoted includes fails, the compiler must fall back on the implementation-defined method used for resolving angle-bracketed includes.

两种形式的#include指令就是这种情况。特别是,尽管实现通常执行某种相对搜索以查找使用双引号包含语法指定的文件,但C不需要这样。它只需要,如果用于解析双引号包含的实现定义机制失败,编译器必须依赖于用于解析包含角度括号的包含的实现定义方法。

Moreover, C specifies behavior only for the case that the given header name uniquely identifies a file to include. Depending on how one construes "uniquely", one might claim that C does not define any behavior at all in the situation you describe, on the basis that you have not uniquely identified the header you want to include. That's a bit wild, though -- I think it's better to interpret "uniquely" in light of the implementation-defined method for locating headers.

此外,C仅针对给定标头名称唯一标识要包括的文件的情况指定行为。根据一个构造“唯一”的方式,可能会声称C在您描述的情况下根本没有定义任何行为,因为您没有唯一标识要包含的标头。虽然这有点疯狂 - 我认为根据实现定义的定位头的方法来解释“唯一”更好。

Your best bet is to avoid header names that collide with standard header names. One version of that would be to put them in a subdirectory that will lend its name as a prefix. For example, put string.h in src/myapp/, and include it as

最好的办法是避免与标准标题名称冲突的标题名称。其中一个版本是将它们放在一个子目录中,该子目录将其名称作为前缀。例如,将string.h放在src / myapp /中,并将其包含为

#include "myapp/string.h"

To make that as safe as possible, ensure that directory src/ is in the include search path.

要使其尽可能安全,请确保目录src /位于包含搜索路径中。

#2


1  

Upon viewing your gist I noticed a discrepancy from your screenshot:

在查看您的要点后,我发现您的屏幕截图存在差异:

HEADER_SEARCH_PATHS = /Users/thomas/SourceTree/SUbD/SUbD/../ThirdParty/include/ruby/mac /Users/thomas/SourceTree/SUbD/SUbD/../ThirdParty/include/ruby/mac/universal-darwin12.5.0

This should be:

这应该是:

HEADER_SEARCH_PATHS = /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include