47

In Xcode, is there a way to specify that all files in a folder are compiled by a target. Eg. the 'Test' target automatically compiles all files in the 'Tests' folder, whereas the 'App' target compiles everything in the 'Sources' folder.

Today, the way I'm doing it, is to add a file to a target every time I create it. This feels a bit error prone and redundant since the files are already organized in the correct folders.

Thanks.

shim
  • 9,289
  • 12
  • 69
  • 108
Akshay Rawat
  • 4,714
  • 5
  • 40
  • 65
  • Folder-reference does not work since years; **see it the way Apple does:** "If we allow files from folder-reference to compile, people may create structure in Xcode once, and develop with some-other IDE" (more 3rd-party IDE options means less money for Apple). – Top-Master Jul 24 '22 at 06:51
  • "more 3rd-party IDE options means less money for Apple" Yeah, because we pay so much money for Xcode. – Michael Long Sep 11 '22 at 16:48

9 Answers9

21

This is completely broken in Xcode. Adding folders by folder reference (@Pavel's answer) should be correct - but it just doesn't work. See also https://stackoverflow.com/a/42600782/2518722. I'm using Xcode 8.3 (update: still broken in Xcode 11), but it's been broken for many generations prior, too.

There isn't a perfect workaround, but this is what I do:

  1. Drag the directory into the project
  2. When asked, set options as follows:
    • Select Create groups for any added folder (ie, the opposite to what you think would be right)
    • Deselect Copy files if needed (because you want a link, not a copy).

Now any changes to files in the shared directory will be reflected in Xcode, which is good.

But, there's no way of refreshing the group. (Someone please tell me if I'm missing something obvious here.) So if you've added new files to the directory, you have to remove the group in Xcode and re-add it before those files show up. This is obviously inconvenient, but at least it's not too prone to user error - and is better than adding files manually one-by-one.

Have to say I find it pretty unbelievable that such a simple and necessary feature of a development environment is so broken here.

BadPirate
  • 25,802
  • 10
  • 92
  • 123
HughHughTeotl
  • 5,439
  • 3
  • 34
  • 49
  • Is there a radar for this bug? I'm assuming it's still broken in XCode 9. – rmooney Dec 04 '17 at 16:21
  • I haven't checked in Xcode 9. To be honest I find Xcode so completely full of bugs, many of which go reported but never addressed, that I've lost confidence in Apple's development process entirely. – HughHughTeotl Dec 05 '17 at 11:12
  • Good workaround for an inexcusable situation, got me working without having to add files one by one or copy them. – Bogatyr Oct 31 '19 at 07:31
  • Not broken - because it was never working that way, and is out of Xcode definitions. – Motti Shneor Jan 27 '20 at 08:45
  • By the way you removed/re-added the group - you could remove/re-add the new sources... participation of sources in the target is STATIC, and determined AT THE TIME YOU DRAG the folder (or separate sources - no difference) to Xcode'w project window. – Motti Shneor Jan 27 '20 at 08:47
  • @MottiShneor Do you mean that, for example, once the group is added then you can add a new source file just by dragging it in? Might be helpful in some cases but too prone to human error for my liking in the case of large directories and adding/removing multiple files. Better to delete and re-add the group. – HughHughTeotl Jan 28 '20 at 17:05
  • 2
    @MottiShneor That is a curious definition of not broken. It is _worse_ to say that xcode were intended to work this way (broken _requirements_ / _design_) then to straight up recognize it as a bug – WestCoastProjects May 09 '20 at 20:27
  • Thanks, this means it's not only me which have this problem :/ – Viktor Sehr Jun 04 '20 at 14:03
9

When you add files to Xcode project, you will have two options to choose from in files select dialog:

  1. Create groups for any added folders.
  2. Create folder references for any added folders.

You need to select the second option. In this case, Xcode will always reflect the changes to files and subfolders of selected folder.

It seems the question was asked a long time ago, but it's still on first place in google search, so hopefully it will be helpful for someone.

iHarshil
  • 739
  • 10
  • 22
Pavel
  • 920
  • 8
  • 16
  • 13
    Unfortunately it doesn't work anymore. Xcode doesn't add folder reference to "compile sources". Not sure about Xcode 4, but Xcode 5 it is definitely not working. Apple removed a lot of handy features, e.g. this one and dragging folders to another project. – superarts.org May 29 '14 at 03:58
  • In XCode 7.3 you should add the folder and before pressing the you have on the left side "Options" button. you should press on it and choose the option Create groups for any added folder. – user2067656 Aug 25 '16 at 05:15
  • **See it the way Apple does:** "If we allow files from folder-reference to compile, people may create structure in Xcode once, and develop with some-other IDE" (more 3rd-party IDE options means less money for Apple). – Top-Master Jul 24 '22 at 06:51
5

You can add multiple files to targets with this. But it requires some work in filtering what files you want.

Project > Target > Compile Sources > cmd+A to select all files > filter for .m files > click "Add"

Similar to other resources,

Project > Target > Copy Bundle Resources > filter for .png .wav .strings .xib .storyboard, etc. > click "Add"

Hlung
  • 13,850
  • 6
  • 71
  • 90
2

Here are some other questions on stackoverflow attempting to deal with this issue:

Add files to an Xcode project from a script?

Automatically adding generated source files to an xcode project

Community
  • 1
  • 1
ThomasW
  • 16,981
  • 4
  • 79
  • 106
  • 1
    None of these are relevant here. The first one is about resource files and Akshay asked about **source files**. The second one is not relevant either, the link for the project is dead and it does not play well with dependency tracking; modifying an Xcode project after the build has been started is like patching your binary compiler while it is compiling a source file. It might work, though I would not rely on that for a single second. – Mecki Apr 18 '12 at 19:10
  • @Mecki The minit project was deleted, but the https://github.com/expanz/xcode-editor project is around. Modifying an Xcode project is more like modifying a make file, perhaps not the safest thing to do, but doable. With the right combination of tools and processes it will work. It depends on what you want to do. – ThomasW Apr 18 '12 at 23:54
1

I just sort by type, so all my source files are listed consecutively.

Then I hold Shift+Cmd and press the down arrow key to select all files I want.

Once selected I can just check all to add to a specific target :)

Moshe Rabaev
  • 1,892
  • 16
  • 31
0

One approach you could take is to use CMake to generate the Xcode target for you.

Detail: CMake - Automatically add all files in a folder to a target?

Another potential avenue would be to configure a custom build phase.

Community
  • 1
  • 1
justin
  • 104,054
  • 14
  • 179
  • 226
0

Yes - just select all the files in the folder and use the inspector window (targets tab).

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 5
    But with this, every time a new file is created, the target will need to be specified. What would be nice is, if there was a way of auto setting this target based on the folder in which the file is in. Then, there would be no need to assign targets to files everytime, since they would be in the correct folder. eg. Test, App example from above. – Akshay Rawat May 19 '10 at 20:29
  • @Akshay: I see what you're saying - yes, that might save a little time. You can use the Apple Bug Reporter to make feature requests such as this. – Paul R May 19 '10 at 20:50
  • 2
    just found this from googling - there HAS to be a way to do this, i bet. looking to do the same thing. – taber May 19 '10 at 23:46
  • You might try asking on Apple's Xcode mailing list: "Xcode Users" – Paul R May 20 '10 at 05:16
0

As far as I know, there is no bug in Xcode regarding this, and Xcode never supported such a mechanism (automatically adding/removing sources from a target based on availability in file-system).

The Group variations (groups-representing-a-folder vs. "regular" groups) merely allow you flexibility in how your project tree looks visually - should it closely follow your repository on disk, or be more "human-readable" and descriptive - regardless of the placement of sources in the repository.

I'm speaking of all Xcode versions I worked with - i.e. v1.0b2 to v11.3

In any way, I admit it WOULD BE NICE to have such a mechanism as a new feature (we could pledge Apple for this).

A way to do it now, could be to build a tiny new "Compiler" whose input is a Folder path, and whose "Generated code" would be the set of source files in that folder. If we add such a 'compiler' as a build rule to our target --- we might achieve the desired effect.

If you want to dig in this, follow the way Protobuf can be added into Xcode projects, and how CoreML models are handled in an Xcode target.

Motti Shneor
  • 2,095
  • 1
  • 18
  • 24
-1

Xcode is extensible using shell scripts. You can set the shell script to run at build time so you can basically make xcode do anything you want. You're only limited by your shell scripting abilities and I'm sure you can find help for that too.

regulus6633
  • 18,848
  • 5
  • 41
  • 49
  • How exactly would that look like? Of course it should not recompile all files all the time, etc. – Albert Jul 25 '10 at 14:14