59

I'm working on an iOS app project, and add the json-framework project to the workspace. The project navigator on the left shows both projects, and the build scheme selector shows the schemes from both projects too. Now I want to add the libjson.a target from the json-framework project as a dependency on the iOS app target in the other project. The expected result is that whenever the app target is built, it builds (if necessary) the library target and links the app target against it. Here are the ways I've tried to do this:

  • Build both as part of the same scheme. The way I try this is to edit the scheme for my app, adding 'libjson.a' to the 'Build' portion of the scheme, and by the way "Find Implicit Dependencies" is checked. Then I go to the target editor for my app target, and in "Build Phases"->"Link Binary With Libraries", I choose 'libjson.a' from the list of workspace libraries. When I subsequently try to build the scheme, I see it build the library target, but building the app target fails with linker error "Library not found for -ljson" - suggesting that it hasn't actually discovered that the library has been built. Indeed in the project navigator, the entry under the app project for the library is still red indicating that the file doesn't exist.

  • Add the json target as an explicit dependency. To try this, I don't modify the build scheme, but go to the target editor for my app target and click the add button under 'Target Dependencies'. No targets from other projects in the workspace show up, so this is a non-starter.

  • Drag the JSON project into the other project, then add the target as a dependency. This is what I would have done in Xcode 3. In the project navigator, I grab the library project and drag it over the app project. This brings up the usual 'add files' pane, which I just dismiss by clicking 'Finish'. There are now two entries for the library project in the project navigator: one at the top level, and one under the app project. I can now add the library target as a dependency of the app target using the target editor, and can link against it without error in the link libraries phase. But it looks broken: there are multiple entries for the same project in the navigator. Is there a different way to do this?

What should be considered the "Xcode 4-ish" way of connecting these targets in different projects in the same workspace? It would seem lacking if multiple projects in the same workspace can't actually interact with each other. Thanks, Graham.

7 Answers 7

24

I’ve just set a test project up, pretty much as you describe in version 3, by creating a new workspace and dragging the two Xcode project into it, nested as shown.

You can delete the sibling project if you have it already.

Hitting build on this and it just works, as far as I can see.

Scheme Settings Project Layout

I imagine there is internal path-confusion if you have two projects, and I’d be inclined to fiddle with location settings in "View"->"Utilities"->"File Inspector" and see what effect that has.

enter image description here enter image description here

Another thing to try is to set your paths up in Xcode "Preferences…"->"Source Trees" and refer to them that way, as described here: Easy, Modular Code Sharing Across iPhone Apps: Static Libraries and Cross-Project References

HTH. Andy W.

5
  • I hope that works on both our machines! :) I have seen exactly the same setup not work, which I put down to general Xcode 4 flakiness.
    – creednmd
    Mar 31, 2011 at 11:00
  • I was having the same problems, and fixed them by editing the scheme and turning off paralyzed builds for both build and archive. That fixed the problem for me. Jul 3, 2011 at 1:33
  • 8
    what's the point of Xcode 4 workspace if you have to use subproject anyways?
    – Tony
    Jan 3, 2012 at 21:31
  • Workspaces are useful to group related stuff together. Makes building targets, handling sub-projects, libraries’ build paths and schemes more convenient. It’s more about organisation than anything else, so you may see a benefit or you may not, depending on how you work.
    – creednmd
    Jan 6, 2012 at 20:33
  • actual link to the blogpost: blog.sina.com.cn/s/blog_79a2178d0100ypfn.html
    – joliejuly
    Nov 24, 2020 at 17:44
9

I managed to get dependencies between projects in a workspace to work as I described here: http://blog.carbonfive.com/2011/04/04/using-open-source-static-libraries-in-xcode-4/.

Unfortunately I can't find a way to get Xcode to discover implicit dependencies or index everything in the build as advertised. I found workaround to both but I'm hoping that less manual configuration will be needed as Xcode 4 matures.

2
  • Superb blog entry! Helped me a lot! +1
    – leviathan
    Aug 3, 2011 at 17:54
  • 2
    In 4.2 the process was almost flawless on a very complicated project setup (many library and application subprojects containing one or two targets). The only thing that didn't work off the bat was linking. I had to add "$(BUILD_ROOT)/$(CONFIGURATION)" to the library search path, so that all the products from the library sibling projects could be found. I was pointed in the right direction by your section "Adding the static library’s headers". Thanks! Dec 12, 2011 at 9:50
1

I was going to ask the same question, thinking that my own solution couldn't be right. But I don't see it mentioned here, and it does seem to work. Clearly XCode 4 is a work in progress. :)

I have a workspace with two projects: a static library and an app which uses the library. The projects are siblings. Each project has its own scheme, and each scheme is set to only build one target. In other words, I added two projects to the workspace and that's it.

To add the static library as a dependency of the app, I just drag the libsomething.a product from the library project (Project Navigator) into the "Link Binary with Libraries" list for the app target. That's it. Now when I build the app the library project is built first and then linked. Interestingly, when I modify the app's scheme to use a different configuration (eg, Release instead of Debug), the library is built using the same configuration.

So it works, and there is clearly some automatic dependency checking going on here. But it feels wrong. Then again, so does the modal scheme editor/manager and lack of a workspace object in the project navigator... I never thought I'd say it, but the Visual Studio UI (bleh) is a lot clearer.

1

My bullet-proof solution to do this :

Create "Per Debug-Release / Per Architecture" settings in Build Settings in the Main project (not the lib), to include either ../MyLibProject/build/Debug-iphoneos or ../MyLibProject/build/Release-iphonesimulator or etc..
depending on the configuration (you can create those kind of configuration by clicking on the + next to Debug or Release and choose either "Any iOS Simulator SDK" or "any iOS SDK". You need to do that for both "Header Search Path" (in case your library copy some headers files, which is more than likely) AND for "Library Search Paths". Which means that for each setting, you'll probably end-up with 4 different paths (debug sim , debug ios, release sim, release ios). That would make sure the configuration of both projects match.

Now, to auto-compile the lib, that is to create the dependency, you can use the "Build Phase -> Link to Binary With Libraries -> + -> select the .a file" advice given above.

That's the only way I managed to have something that builds and link correctly for every environment on xcode 4.5

Note : I even added the -lmyLib flag in "other linker flags", but i'm not sure that's really necessary

0

I've had some success with creating framework-like static libraries, though it's not a perfect solution.

0

I see the next variants:

  1. Explicit dependency in a project[About]
  2. Implicit dependency in a workspace[About]
-1

See the Xcode user guide: Xcode Concepts -> Xcode workspace under 'Projects in a Workspace Share a Build Directory'.

All projects in a single workspace share a build directory. Dependencies are discovered automatically and build if needed:

"Xcode examines the files in the build directory to discover implicit dependencies. For example, if one project included in a workspace builds a library that is linked against by another project in the same workspace, Xcode automatically builds the library before building the other project, even if the build configuration does not make this dependency explicit. You can override such implicit dependencies with explicit build settings if necessary. For explicit dependencies, you must create project references."

5
  • 2
    My question already indicates that this approach doesn't work.
    – user23743
    Mar 30, 2011 at 11:34
  • I've had it work that way, but in a new workspace I created with two projects this doesn't seem to work anymore. I still think this is the 'Xcode 4-ish' to work with dependencies (which is what you asked for), bugs probably just prevent the method from working correctly. Seems the only way to do this now is the ugly technique. Mar 30, 2011 at 12:07
  • Scratch my last comment, implicit dependencies do seem to work. I created a test workspace + two projects (library + app using the library). Building the app will automatically build the library. Project was created similar to the first bullet point. Clone the code at: bitbucket.org/kluivers/dependencies-demo Mar 30, 2011 at 12:46
  • 1
    Sometimes they do, sometimes not. Your project works for me. But I've created (in XCode 4) an almost identical one, which doesn't work (or at least not consistently: the first build after a clean is generally OK, but subsequent builds don't rebuild the library to reflect new changes).
    – Cris
    Apr 3, 2011 at 21:19
  • This will work every time, but only if you make a correction to your project requiring you to edit the .pbxproj directly. I'm using this technique, and I'm not sure it's really worth it. Oct 30, 2012 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.