Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of Compile Sources in Xcode?

Tags:

xcode

ios

  • 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.

enter image description here

like image 662
drc Avatar asked Jun 19 '13 18:06

drc


1 Answers

You asked

What is the purpose of Compile Sources in 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.

A couple of practical examples of when you might edit your "Compile Sources":

  1. 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) remove fmdb.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 SDWebImage framework 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 the MKAnnotationView 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 the MKAnnotationView+WebCache from your compile sources and the need to link the MapKit.framework (when you might not otherwise need it) is resolved.

  2. 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.

  3. 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.

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.

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.

add targets

like image 183
Rob Avatar answered Sep 26 '22 01:09

Rob