Xcode - Automatically add all files in a folder to a target

Akshay Rawat picture Akshay Rawat · May 19, 2010 · Viewed 31.3k times · Source

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.

Answer

HughHughTeotl picture HughHughTeotl · Apr 6, 2017

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.