在 Xcode 中编译源的目的是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17198185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the purpose of Compile Sources in Xcode?
提问by drc
- What is the purpose of Compile Sources in Xcode?
- Does every file in your project need to in there?
- If I add files to a project, does every file get added to compile sources.
- 在 Xcode 中编译源的目的是什么?
- 项目中的每个文件都需要在那里吗?
- 如果我将文件添加到项目中,是否每个文件都被添加到编译源中。
回答by Rob
You asked
你问
What is the purpose of Compile Sources in Xcode?
在 Xcode 中编译源的目的是什么?
The purpose is to inform the compiler which source files (e.g. .m files) should be compiled in the process of building a target.
目的是通知编译器在构建目标的过程中应该编译哪些源文件(例如.m 文件)。
A couple of practical examples of when you might edit your "Compile Sources":
关于何时可以编辑“编译源”的几个实际示例:
In addition to Idles' example of including or excluding sources for multiple targets in your project, another real-world scenario where you might be excluding sources from "Compile Sources" is when you're using third party library (or set of classes) that provides source code, but you want to control which sources will be compiled in your project.
For example, if using FMDB, you copy the sources to your project, but that includes a sample
fmdb.m
file that illustrates how to use FMDB, but you don't want to compile that as part of your app. You could (maybe even want to) removefmdb.m
from your project altogether, but you might also just remove that one file from your compile sources, that way you have a nice example of how to use the classes at your fingertips, but it won't be compiled as part of your project.Another real-world example would be if you use the SDWebImageframework and have included the sources in your project. You might, though, not need the
MKAnnotationView+WebCache
category, in which case you would probably want to remove it from your "Compile Sources" if not using maps and theMKAnnotationView
class. If you kept that in your project, you might get linker warnings if you didn't also link your project with the MapKit.framework. If you're not using MapKit, it would be easier to just remove theMKAnnotationView+WebCache
from your compile sources and the need to link the MapKit.framework (when you might not otherwise need it) is resolved.Another time you'll use the "Compile Sources" is when you want to set a compiler flag for just some particular source file. For example, if you have an ARC project, but you have some non-ARC code. You can go into "Compile Sources", select the non-ARC .m file, and set the
-fno-objc-arc
flag as outlined in the Transitioning to ARC Release Notes.If you ever added files to your project, but accidentally neglected to select the "Add to targets" option, those source files would not be compiled when you build your target. But you could go into "Compile Sources" and add those source files in the list of files to be compiled and they would subsequently be included in future builds of your target.
除了 Idles 在项目中包含或排除多个目标的源的示例之外,另一个可能从“编译源”中排除源的实际场景是当您使用第三方库(或一组类)时提供源代码,但您希望控制将在您的项目中编译的源代码。
例如,如果使用FMDB,您将源代码复制到您的项目,但其中包含一个示例
fmdb.m
文件,说明如何使用 FMDB,但您不想将其编译为应用程序的一部分。您可以(甚至可能想要)fmdb.m
从您的项目中完全删除,但您也可以只从编译源中删除该文件,这样您就有了一个很好的示例来说明如何使用这些类触手可及,但它不会作为项目的一部分进行编译。另一个实际示例是,如果您使用SDWebImage框架并在您的项目中包含源代码。但是,您可能不需要
MKAnnotationView+WebCache
类别,在这种情况下,如果不使用地图和类,您可能希望将其从“编译源”中删除MKAnnotationView
。如果您将其保留在您的项目中,并且您没有将您的项目与 MapKit.framework 链接,则您可能会收到链接器警告。如果您不使用 MapKit,MKAnnotationView+WebCache
则从编译源中删除 会更容易,并且解决了链接 MapKit.framework(当您可能不需要它时)的需要。另一次您将使用“编译源”是当您只想为某些特定源文件设置编译器标志时。例如,如果您有一个 ARC 项目,但您有一些非 ARC 代码。您可以进入“编译源”,选择非 ARC .m 文件,并
-fno-objc-arc
按照过渡到 ARC 发行说明中的概述设置标志。如果您曾向项目添加文件,但不小心忽略了选择“添加到目标”选项,则在构建目标时将不会编译这些源文件。但是您可以进入“编译源”并将这些源文件添加到要编译的文件列表中,它们随后将包含在您目标的未来版本中。
You asked:
你问:
Does every file in your project need to in there?
项目中的每个文件都需要在那里吗?
No, just every source file (e.g. .m file) that you want to be compiled. Not headers. Not assets. Just source code you want compiled.
不,只是您想要编译的每个源文件(例如 .m 文件)。不是标题。不是资产。只是你想要编译的源代码。
If I add files to a project, does every file get added to compile sources?
如果我将文件添加到项目中,是否每个文件都被添加到编译源中?
If you click the "Add to targets" checkbox when adding the files, the .m files will be added to "Compile Sources" and the resources will be added to "Copy Bundle Resources", etc. Otherwise they won't.
如果在添加文件时单击“添加到目标”复选框,.m 文件将被添加到“编译源”,资源将被添加到“复制捆绑资源”等。否则它们不会。
回答by Idles
Say you've got multiple targets in your project and you want only some of your code in each target, then you'd remove/add things to the compile sources as appropriate. It's basically a list of source code files that will be passed to the compiler when building that target.
假设您的项目中有多个目标,并且您只需要每个目标中的一些代码,然后您将根据需要将内容删除/添加到编译源中。它基本上是在构建该目标时将传递给编译器的源代码文件列表。
For example, I might set up a target that builds subset A of my code into a static library, then another target that builds subset B, links against the library built by the other target, and specifies the first target as a target dependency so that if I build target B, Xcode will build target A first.
例如,我可能会设置一个目标,将我的代码的子集 A 构建到静态库中,然后另一个目标构建子集 B,链接到另一个目标构建的库,并将第一个目标指定为目标依赖项,以便如果我构建目标 B,Xcode 将首先构建目标 A。