iOS添加pch头文件

pch头文件是Precompile Prefix Header,指的是预编译头文件,用于存放项目公共的头文件,方便在开发阶段一次性导入多个头文件。本篇文章介绍如何在iOS项目中添加pch文件。

1.新建pch文件

点击File-New-File…,选择iOS下面的PCH File

请添加图片描述

文件自动命名为PrefixHeader,可以拖入工程代码文件夹下面,文件内容可以加入工程里面公共的头文件,比如#import <UIKit/UIKit.h>

#ifndef PrefixHeader_pch
#define PrefixHeader_pch

// Include any system framework and library headers here that should be included in all compilation units.
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.

#import <UIKit/UIKit.h>

#endif /* PrefixHeader_pch */

2.配置pch路径

在build settings里面搜索apple clang - Language,修改Precompile Prefix Header为YES,

Prefix Header修改为$(SRCROOT)/iOSProject/PrefixHeader.pch

$(SRCROOT)代表工程根目录,iOSProject代表目录名,根据自己的目录名称修改

请添加图片描述

3.优化pch文件

在一些工程项目中需要和C/C++混合编译,C/C++也会自动引入pch头文件,这个时候工程编译会报错,可以在pch文件中加入预编译指令#ifdef __OBJC__避免这个问题,注意OBJC前后分别是两个下划线,这样配置后,只有Objective-C的文件才会实际使用到pch文件。

#ifndef PrefixHeader_pch
#define PrefixHeader_pch

// Include any system framework and library headers here that should be included in all compilation units.
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#endif

#endif /* PrefixHeader_pch */
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值