How to Create Static Library and Can Add Just .A File on Any Project in iOS

How to build a static library of an existing project in objective-c (.a file)?

http://mobileorchard.com/sharing-compiled-code-building-static-libraries-in-xcode-for-the-iphone/ has a good tutorial.

Here are the basic steps:

  1. Understand that XIB/NIB/Storyboard files have to be included separately from the library
  2. Add a new target that is a static library
  3. In the build settings for that library, include all of the .h and .m files that are relevant. For example, if your app uses a set of classes to access data and another set to view that data, you may want to just include the files relevant to accessing data.
  4. Build. Use the script provided at Build fat static library (device + simulator) using Xcode and SDK 4+ so that you can use it for iPhone simulator + device.
  5. Include your library + headers + any resources in relevant project.

Import a static library as a subproject

You must add Header Search Path to your target. In build setting search "Header Search Path" and add relative link to your static library project. How to add relative path read more here

You may need to search path with recursive option.

Any way to make bundle xcode project to include a static library xcode project?

Finally, I make it work.

The step 10 still important and below is corrected step 10.

  • create a dummy.c under (.bundle) project and the dummy.c can just totally empty. remove the setting for the library you want to link inside Link Binary With Libraries instead use -Wl,-force_load,$(CONFIGURATION_BUILD_DIR)/libYourLib.a or -all_load to Other Linker Flags

PS: And also can use sub-project instead of workspace. and use Target Dependencies instead of Edit Scheme to achieve the same effect.

The testing project

add static library to xcode project from command line

Not a directly command line way, but you can write a ruby script to do the trick. You can use a gem called xcodeproj to edit the Xcode project file.

For adding a .a library, you may want to create an Project object with the path of your project file, then use the new_file method of the Project class to add it to the project. It is equivalent to dragging the same file to Xcode. For the header file, you can either add them as new file to the project (the same as the library) or just set the HEADER_SEARCH_PATHS key of the Xcodeproj::Project::Object::XCBuildConfiguration of the project object.

For more information and usage of xcodeproj, you can refer to the home page and the doc page.

Linking a static library to an iOS project in Xcode 4

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.



Related Topics



Leave a reply



Submit