添加pch文件
2015-07-10 09:44
267 查看
在Xcode6之前,新建一个工程的时候,系统会帮我们自动新建一个以工程名为名字的pch (precompile header)文件,在开发过程中,可以将那些整个工程都广泛使用的头文件包含在该文件下,编译器就会自动的将pch文件中的头文件添加到所有的源文件中去,这样在需要使用相关类的时候不需要使用import就可以直接使用头文件中的内容,很大程度上带来了编程的便利性,但潜在的也带来了一些问题,这也是在Xcode6中默认不再创建pch的原因吧。
关于pch的得与失,stackoverflow上有段话讲的比较透彻:http://stackoverflow.com/questions/24158648/why-isnt-projectname-prefix-pch-created-automatically-in-xcode-6
As to where to put code that you would put in a prefix header, there is no code you should put in a prefix header. Put your imports into the files that need them.
Put your definitions into their own files. Put your macros...nowhere. Stop writing macros unless there is no other way (such as when you need __FILE__). If you do need macros, put them in a header and include it.
The prefix header was necessary for things that are huge and used by nearly everything in the whole system (like Foundation.h). If you have something that huge and
ubiquitous, you should rethink your architecture. Prefix headers make code reuse hard, and introduce subtle build problems if any of the files listed can change. Avoid them until you have a serious build time problem that you can demonstrate is dramatically
improved with a prefix header.
In that case you can create one and pass it into clang, but it's incredibly rare that it's a good idea.
但有些时候,还是需要pch文件的,那么怎么在Xcode6中添加一个pch文件呢?
首先,Command+N,打开新建文件窗口:ios->other->PCH file,创建一个pch文件,添加需要引入的头文件名:
其次,修改工程配置文件,将刚刚创建的PCH file的路径添加到building setting中的precompile header选项中去,注意debug和release两栏都要添加:
至此,大功告成,编译一遍,新添加的pch文件就可以正常使用了^_^。
关于pch的得与失,stackoverflow上有段话讲的比较透彻:http://stackoverflow.com/questions/24158648/why-isnt-projectname-prefix-pch-created-automatically-in-xcode-6
As to where to put code that you would put in a prefix header, there is no code you should put in a prefix header. Put your imports into the files that need them.
Put your definitions into their own files. Put your macros...nowhere. Stop writing macros unless there is no other way (such as when you need __FILE__). If you do need macros, put them in a header and include it.
The prefix header was necessary for things that are huge and used by nearly everything in the whole system (like Foundation.h). If you have something that huge and
ubiquitous, you should rethink your architecture. Prefix headers make code reuse hard, and introduce subtle build problems if any of the files listed can change. Avoid them until you have a serious build time problem that you can demonstrate is dramatically
improved with a prefix header.
In that case you can create one and pass it into clang, but it's incredibly rare that it's a good idea.
但有些时候,还是需要pch文件的,那么怎么在Xcode6中添加一个pch文件呢?
首先,Command+N,打开新建文件窗口:ios->other->PCH file,创建一个pch文件,添加需要引入的头文件名:
其次,修改工程配置文件,将刚刚创建的PCH file的路径添加到building setting中的precompile header选项中去,注意debug和release两栏都要添加:
至此,大功告成,编译一遍,新添加的pch文件就可以正常使用了^_^。
相关文章推荐
- 踩点记录博客开通
- 国家气象局提供的天气预报接口
- 4818:【一维数组】元素排序 分数: 2
- 1021:恶搞指数
- 1019:石头剪子布 分数: 1
- 1021:恶搞指数
- 1019:石头剪子布 分数: 1
- 1018:奇怪的信 分数: 1
- 1016:计算利息 分数: 1
- 1036:镂空三角形
- 3813:【第七章数组】洗牌问题 分数: 3
- 4814:【一维数组】数组元素输出 分数: 1
- 4854:【字符数组】字符输出 分数: 1
- 竞赛题库题解1015
- 1014:当总统 分数: 1
- 1048:字符串反转 分数: 1
- 1013:摆积木 分数: 1
- 1034:打印数字图形 分数: 1
- 4819:【一维数组】陶陶摘苹果 分数: 1
- 陶陶摘苹果