import 双引号与尖括号的区别以及build setting 中一些 header search path 配置的说明

import "xxx.h"的路径搜索顺序:

1. USE_HEADERMAP(如果启用,则会在映射表中查,直接跳过的header search path的配置,如果未查到,则继续往下搜索。)
2. USER_HEADER_SEARCH_PATHS
3. HEADER_SEARCH_PATHS

import <xxx.h> 的路径搜索顺序:

1. 系统路径
2. USER_HEADER_SEARCH_PATHS (ALWAYS_SEARCH_USER_PATHS 为 YES,则会搜索该路径,该变量默认是NO,并且已经被标记为Deprecated)
3. HEADER_SEARCH_PATHS

下面对上面的搜索路径做一些说明:

USE_HEADERMAP 是什么意思?

官文如下:

Use Header Maps (USE_HEADERMAP)

Enable the use of Header Maps, which provide the compiler with a mapping from textual header names to their locations, bypassing the normal compiler header search path mechanisms. This allows source code to include headers from various locations in the file system without needing to update the header search path build settings.

大致意思就是:如果启用了这个设置,那么系统会为编译器提供一个 “header名称 -> 文件路径” 的映射,这样源码中引用了的头文件就可以直接通过这个映射关系查找到对应的文件路径,这样就可以绕过编译器正常情况下的头文件搜索机制。

那么Use Header Maps的效果是怎么样的呢?下面用一个demo演示下:
新建一个 MainPrj(iOS Single View App类型),在这个项目中新建一个静态库的target XX,完毕后项目目录如下:
这里写图片描述

下面我们在 MainPrj 中的 ViewController.m 添加一句引用 #import "XX.h",此时编译是能够通过的,但是我们的主项目没有配置 HEADER_SEARCH_PATHS 和 USER_HEADER_SEARCH_PATHS,那为啥能够
找到这个头文件的?其实这里就是USE_HEADERMAP在起作用,我们可以试着把主项目中的USE_HEADERMAP配置为NO,然后就无法编译通过,会提示找不到头文件。

另外,在 Xcode help中,找到下面三个环境变量:

HEADERMAP_INCLUDES_FLAT_ENTRIES_FOR_TARGET_BEING_BUILT

Specifies whether the header map contains a name/path entry for every header in the target being built.

HEADERMAP_INCLUDES_FRAMEWORK_ENTRIES_FOR_ALL_PRODUCT_TYPES

Specifies whether the header map contains a framework-name/path entry for every header in the target being built, including targets that do not build frameworks.

HEADERMAP_INCLUDES_PROJECT_HEADERS

Specifies whether the header map contains a name/path entry for every header in the project, regardless of the headers’ target membership.

看字面说明应该是影响USE_HEADERMAP变量作用的文件范围的,不过在Xcode Build Setting中并未找到相关设置(默认是True, 可以在Build Phases
添加一个自定义脚本,脚本内容随便,然后勾选Show environment variables in build log,之后build完后,在build日志中能够找到这三个环境变量)。

ALWAYS_SEARCH_USER_PATHS

官文如下:

If enabled, both #include <header.h>-style and #include “header.h”-style directives search the paths in User Header Search Paths (USER_HEADER_SEARCH_PATHS) before Header Search Paths (HEADER_SEARCH_PATHS). As a consequence, user headers, such as your own String.h header, have precedence over system headers when using #include <header.h>. This is done using the -iquote flag for the paths provided in User Header Search Paths. If disabled and your compiler fully supports separate user paths, user headers are only accessible with #include “header.h”-style preprocessor directives.

For backwards compatibility reasons, this setting is enabled by default. Disabling it is strongly recommended.

大意是如果ALWAYS_SEARCH_USER_PATHS被设置为YES,无论#include <header.h>还是#include "header.h"的形式,都会先搜索User Header Search Paths目录。
不过这个变量已经过时了,目前还存在只是为了向后兼容,默认值都是被设置为NO。

系统路径到底是什么路径?

可以在Build Phases -> Link Binary With Libraries添加一些静态库和动态库依赖,然后Show In Finder查看具体路径。路径是下面这个样子:
$DEVELOPER_DIR/Platforms/具体的平台/Developer/SDKs/平台sdk/System/Library/
$DEVELOPER_DIR/Platforms/具体的平台/Developer/SDKs/平台sdk/usr/include

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jerryandliujie/article/details/79965969
文章标签: iOS import
个人分类: iOS-问题集
上一篇(译)File System Programming Guide之Techniques for Reading and Writing Files Without File Coordinators
下一篇GCDWebServer 源码解读
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭