17

How can I create static library and can add just .a file on any project in ios.

I tried doing this but couldn't do it.

Thanks in advance

Lebyrt
  • 1,398
  • 1
  • 9
  • 18
  • 1
    possible duplicate of [IOS Static library how to, and issues](http://stackoverflow.com/questions/5653374/ios-static-library-how-to-and-issues) – Amar Jul 11 '13 at 09:15
  • Hi, no. in the example you had sent adds the whole project. I just wants to add .a file of library. –  Jul 11 '13 at 09:37
  • 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:52

3 Answers3

30

if you want create static lib mean refer the link http://jaym2503.blogspot.in/2013/01/how-to-make-universal-static-library.html

Step 1 : Create a New Project, Named it "Logger"

Step 2 : Create Classes

You can create as many classes you wants, In our tutorial we will create one class named "Logger". So, now two files should be in our resource. 1. Logger.h 2. Logger.m

Step 3 : Put some useful code into Classes

Step 4 : Create New Target

Create New Target from File Menu.

New Target Select Cocoa Touch Static Library

Step 5 : Add files to Compile Resource

Select "Logger" Target of Static Library Go to Build Phases In Complied Sources section, Add all the .m and .mm files. In Copy Files section, Add all the .h files and resource files.

Build Phases

Step 6 : Compile Project with Static Library Target

Compile Project for iOS Device Compile Project for Simulator You can find two different .a files generated in build folders.

Find .a file

Step 7: Make Static Library Universal

You can find two different library now, one is for simulator and one is for iOS devices.

Create a New Folder and name it LoggerMerge. Copy libLogger.a file of Debug-iphoneos folder to "LoggerMerge" rename it to libLogger_device.a Copy libLogger.a file of Debug-iphonesimulator folder to "LoggerMerge" rename it to libLogger_simulator.a Open LoggerMerge folder with Terminal Fire below command lipo -create "libLogger_simulator.a" "libLogger_device.a" -output "libLogger.a"

Now, you can find libLogger.a in LoggerMerge folder, this is Universal static library file. Now, Just one thing you need is headers, See above screenshot there is folder called include in both build folder. Just copy header file from this folder.

Step 8 : Test Static Library

Create New Project, name it TestLogger Import libLogger.a and header files Import header file "Logger.h" anywhere you want to use Now, Use this Logger class as default practice. In our case, [Logger log:@"Test String"]; Run Project in Simulator and Device both That's it!! You have your own static Library!!

dhaya
  • 1,522
  • 13
  • 21
  • I cannot open the link you sent. –  Jul 11 '13 at 09:12
  • 1
    I cannot find .a file. Where it should be. After I compiled, my library in products folder is still with red color.Why? –  Jul 11 '13 at 09:33
  • hoo that nothing kajal. just connect with device,it will change. – dhaya Jul 11 '13 at 09:43
  • my device is already connected. but still it has same red colour –  Jul 11 '13 at 09:45
  • 1
    try this http://stackoverflow.com/questions/5409015/app-file-appears-in-red-missing-in-new-projects-with-xcode-4/16462217#16462217 – dhaya Jul 11 '13 at 09:56
  • This article also has some interesting thoughts about how to automate the lipo step as an 'Aggregate Target', part of the build phases: http://www.raywenderlich.com/41377/creating-a-static-library-in-ios-tutorial – nazbot Feb 21 '14 at 16:43
11

Step 1: Starting a New Static Library Project

Open XCode and start a new project. Under iOS, select Library and “Cocoa Touch Static Library” say it as "staticlibrary". This will create a nice new project for us that builds a .a file.

Step 2: Code your static library First we need to add some files. Add a new NSObject subclass to your project and name it StaticClass.Then Write some useful code in those files.

Step 3: Building And Distributing Your Library Once you are happy with your library, simply build it in XCode. Obviously, don’t do build and run as you can’t run it (again unless you have a test suite). Now, look under the Products group in XCode and you should see a file called lib(libraryName).a. In our case, it’s libstaticlibrary.a.

Right click on that file and select “Reveal In Finder”. Drag this file into a new folder that you create where you will be bundling all of your library files.Now, do the same with all of the .h files. In our case, just copy StaticClass.h into this new directory. Your directory structure should now look like:

FolderName
|- libstaticlibrary.a
|-  StaticClass.h

Now you can zip this folder and sell it to would-be iOS developers for millions!

Step 4: Linking Your Library In A New Project So now that you have built your shiny new static library, it’s time to test it out in another application.

Create a new View-Based project (or whatever it doesn’t really matter). Name it as Test.

Now, just drag this folder into the project and XCode will set up all of the linking automatically. When prompted to copy, I usually say yes, but you can do whatever you want depending on how you intend on using the library. Sometimes just linking and not copying is far more beneficial if you have multiple projects sharing a single library. It ensures that they all use the most up to date version.

You should now see the .a file along with the header files in the new project.

Step 5: Using The Static Library Code

Now that all of the linking is set up, you just use your library like any other class.

For Further clarifications http://www.raywenderlich.com/41377/creating-a-static-library-in-ios-tutorial hope this tutorial helps for you.

yashwanth77
  • 1,632
  • 1
  • 10
  • 17
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – mah Mar 21 '14 at 11:22
  • Welcome to Stack Overflow! A link to a potential solution is always welcome, but please [add context around the link](http://meta.stackexchange.com/a/8259/169503) so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. Take into account that being _barely more than a link to an external site_ is a possible reason as to [Why and how are some answers deleted?](http://stackoverflow.com/help/deleted-answers). – Xavi López Mar 21 '14 at 11:27
  • 1
    Thanks a lot for helping me as i am new to stack overflow i don't know the pitfalls i have updated my answer based on your request – yashwanth77 Mar 21 '14 at 11:56
  • @yashwanth777 Glad to help a new user get his way around the site. And thanks to you for contributing with this answer. Please, have a look at the [help], it has many insightful sections that will help you get the best out of Stack Overflow. Best luck! – Xavi López Mar 21 '14 at 12:58
-1

Objective-C consumer -> Objective-C static library

Xcode version 10.2.1

Create Objective-C static library

Follow Create Objective-C static library section

Objective-C consumer with Objective-C static library

Follow Swift consumer with Objective-C static library section

Import module to the Objective-C client code[module_name]

@import module_name;

//or umbrella or public header
#import <module_name/module_name.h>

[More examples]

yoAlex5
  • 13,571
  • 5
  • 105
  • 98