0

I have created a C++ (static) library libFoo.a.

I wish to create an iOS ObjC app in XCode that uses this library.

I would like to have both projects side-by-side. So that I can modify the library code, rerun the consumer app, and it will automatically rebuild the library.

I also need to be able to single-step through the library code.

  • do I need to create an Xcode workspace and drop both projects into it?

  • When I build a library and inspect the 'Products' group, it still appears red even though the build succeeded. Is this okay?

  • How do I inform the consumer of the existence of the .a?

    It is building to: /Users/pi/Library/Developer/Xcode/DerivedData/Foo-eedjtiwaewussebwzhplgqpllrnx/Build/Products/Debug-iphonesimulator/libFoo.a

    If I hardcode this into the consumer, will this create trouble for another developer that clones my code?

    Is there a 'right way'(TM) to do it? Maybe building to a set location and using relative paths...

EDIT: Notes:

  • create the new workspace
  • create a new ObjC project & add to workspace when prompted
  • drag-drop myLib.xcodeproj to be a child node of consumer.xcodeproj in project navigator
  • consumer.xcodeproj -> build phases ->
    • ... Target dependencies -> {add myLib.xcodeproj}
    • ... link binary with libraries -> "+" -> { "Add workspace" -> libMyLib.a }
P i
  • 29,020
  • 36
  • 159
  • 267

1 Answers1

1

1) Yes, use a workspace. There are many advantages to doing so. If you open projects independently of a workspace, and one project (your library project) is also contained within another project (your app project) Xcode won't actually open it the second time. The workspace is much better as you can just build your library when you wish while keeping your app project around, or run your library's unit tests. You may also want more apps based on that library, too. They call all live in the same workspace along with your library and you can set up schemes for building any of them (actually any combo of them, even).

2a) Remove the references to the library from your app's project.

2b) Then add the library project file to your app's project (in project navigator). Don't choose "Copy items if needed" during this step, and don't select any of the project's targets. Press the Finish button.

2c) Click on the app project and go to Build Phases. Click on the app target (in the targets list) that you want the library to be a dependency of (I assume it's your main app target, not an extension target of your app, etc...) Click the + button in Target Dependencies section. Choose your library's product in the list.

2d) Verify that in the Link Binary With Libraries section, that your library is there. If not, I've found you can drag it from your project navigator directly into this section.

3) So I assume you ship your app and library together, yes? A developer will see two directories as peers, one for the library, one for the app? The library directory contains all the code and the project file pertaining to the library, and the app directory contains all the code and project file for the app. The workspace typically lives outside of both directories. Include a README file that explains how the developer should add your library to their own app project, though what I described above is quite typical and won't surprise anyone.

Smartcat
  • 2,834
  • 1
  • 13
  • 25