76

I have a project (AQGridView) that compiles to a static library, but I can't seem to add it to my project.

Dragging in the project to my project creates a workspace, and if I try to link the libAQGridView.a file from the DerivedData directory it doesn't recognize it as a library. I'm not sure what I'm doing wrong.

This is the AQGridView project. Does anyone know specifically how to use it in an Xcode 4 project?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Matt Hudson
  • 7,329
  • 5
  • 49
  • 66
  • 1
    You voted me down after 29 up votes, 2 years later? And on top of that the statement you just made doesn't make any sense. – Matt Hudson Apr 23 '13 at 18:17
  • possible duplicate of [How to "add existing frameworks" in Xcode 4?](http://stackoverflow.com/questions/3352664/how-to-add-existing-frameworks-in-xcode-4) – jww Jul 03 '14 at 05:33
  • Cannot be explained better than here: [Linking Static Libraries](http://www.raywenderlich.com/41377/creating-a-static-library-in-ios-tutorial) – Subham93 Jul 24 '14 at 11:19

6 Answers6

95

I do this as follows:

  1. Drag in the static library project. If you have the static library project open in Xcode, close it now.
  2. Select the main project in the project navigator (the project I'm adding the static library to) and in the editor, under the header TARGETS in the left-hand column, select my main project's target and navigate to the Build Phases tab.
  3. Click the "+" for Target Dependencies and add the library icon target dependency from the added static library project.
  4. Click the "+" for Link Binary with Libraries and add the library icon that is under the folder "Workspace".
  5. It may also be necessary to enter a Header Search Path for the headers of the static library project if that is how the headers are linked in the static library project itself.

If you don't see the static library project as nested under the main project in the main project's project navigator, the most likely reason for that is that the static library's own Xcode project is still open. Quit Xcode and open up the main project that has the nested static library project in it without opening up the original static library project itself, and you should see it appearing as a nested project in your main project.

Halle
  • 3,584
  • 1
  • 37
  • 53
  • 18
    This seems like it's going to work, but for some reason the library does not show up under Target Dependencies. It shows up under Link Binary with Libraries, but not Target Dependencies. I can't figure out why it won't show up. – Matt Hudson May 25 '11 at 13:25
  • Silly question, but is the target of the static library project definitely a library? I see that you mentioned it compiles to a library but inside of the static library project does it have a target which is a library? – Halle May 25 '11 at 13:42
  • 3
    Turns out that the project has to be nested, this will fix the Target Dependency showing up. – Matt Hudson May 25 '11 at 13:42
  • Aha, good to know -- is that nested in the sense that the project is below the main project in the hierarchy of the project navigator? I didn't realize it was possible to drag in a cross-project references that wasn't automatically nested. – Halle May 25 '11 at 13:44
  • Part of my problem was that I started with this post: http://stackoverflow.com/questions/6074576/static-library-in-xcode4 Which turns out is not what I'm looking for, but has good information. – Matt Hudson May 25 '11 at 13:55
  • This didn't work well for me in Xcode 4.2 I found using a Workspace as outlined in this article to work the best: http://blog.kihongames.com/2011/labs-xcode-static-library-pitfalls/#comment-1080 – Greg Martin Nov 02 '11 at 21:07
  • Something that will cause the static library project to not appear as nested after being dragged in in > 4.1 is if the static library project is still open in Xcode. I think that Xcode views this as a danger to internal consistency since changes to the same project can occur in two projects simultaneously. If you get the "nothing is nested after drag-in" issue, just quit Xcode and then reopen only the project that is supposed to show the nested static library cross-project reference. – Halle Mar 04 '12 at 12:01
  • I found that I had to (1) drag the library project into the main project, then (2) close both projects (i.e. quit Xcode). Re-opening the main project then showed me the library project's libraries under Target Dependencies and Link Binary with Libraries, which were previously missing. – OldPeculier Nov 16 '12 at 18:56
  • Can you elaborate a bit more on #5? Do you add a "Header Search Path" in the main project, or the library project? If so, what type of path / value do you enter in this setting? – NathanChristie Dec 03 '12 at 16:15
  • 1
    The main project that has to link to the library, and the path to the folder that contains the headers (or the path to the folder than contains all of the source to the library if you want to check the recursive box). – Halle Dec 03 '12 at 16:52
  • 1
    Where are you suppose to drag the static library? There's no panels in my project which will accept a dragged object... – zakdances Mar 18 '13 at 02:21
  • 2
    You don't drag a static library, you drag the static library Xcode project's main blue project icon in the project navigator pane or its Finder icon onto the main blue project icon in the project navigator pane of the Xcode project you are adding it to. – Halle Mar 18 '13 at 13:30
  • So and how do you import the library headers in your classes then? – openfrog Oct 06 '13 at 15:00
  • The *.a files I've added to "Link Binary With Libraries" are red, what does this mean? – boulder_ruby Feb 24 '14 at 12:07
  • The problem for me is that I have my static Library project in the same Workspace as the Project I'm trying to link it to. Dragging the Library project in to the other project just crashes Xcode every time. – Lee Probert Mar 18 '14 at 21:41
  • 1
    I followed the instructions here and it solved all my issues : https://developer.apple.com/library/ios/technotes/iOSStaticLibraries/iOSStaticLibraries.pdf in particular setting the Other Linker Flags property to -ObjC in my main project did the trick. – Lee Probert Mar 19 '14 at 11:09
15

Xcode menu > View > Utilities > File Inspector

Select the static library file, and then set 'File Type' as 'Mach-O object code' in 'Identity and Type'.

Kazuki Sakamoto
  • 13,929
  • 2
  • 34
  • 96
  • Wow, what a weird solution, but totally worked for me. Thanks! My use case was copying a `.a` static library into `Frameworks` on build, and linking against it, which started throwing "image not found" library errors after rearranging my project a bit. Not anymore, thanks to the above. – Markus Amalthea Magnuson Dec 10 '15 at 20:16
5

Find your .a file in finder, and drag it into your project.

Select the 'copy items into destination group's folder (if needed)', and add your headers to your project.

Now, Xcode 4 should automatically link against that framework for you.

Here is what the library should look like in your project:

Cœur
  • 37,241
  • 25
  • 195
  • 267
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
5

Workspaces are supposed to make this easier, but I don't know that they do. The way I do it is create a workspace, add my main project, add my library project. Then go into the main project's build phases and add the library in the "Link binary with libraries" section and add the library. That should be all that is necessary, at least that's my reading, but it isn't. What I do is go into Xcode preferences, then to the Source Trees pane. Add a source tree that points to your static library's headers, then go back to the build settings for your project, then to the Header Search Paths key, and enter ${foo} there, where "foo" is whatever you called the source tree.

That's what works for me, though I don't know if that's the best or easiest way to do it.

Joey Gibson
  • 7,104
  • 1
  • 20
  • 14
  • If you have the newest version of XCode, this is definitely the way to go. Much easier than digging through project internals and build directories. – zakdances Mar 18 '13 at 04:06
3

Follow Apple's documentation.

In brief:

  1. Link your target against the library.
  2. Add -ObjC to 'Other Linker Flags' (OTHER_LDFLAGS) under the target's build settings.

Also, I needed to add the library to the scheme of my main project since the library was not visible in the target's dependencies.

Corin
  • 2,417
  • 26
  • 23
0

The Halle's answer works for me with one addition:

  1. Check in Build Settings of the static library project the Private( or Public) Headers Folder Path value and copy it.
  2. Add copied value to the client project Build Settings Header Search Path or User Header Search Path depending of the include directive you are using
IZIordanov
  • 31
  • 2