Create and use static libraries in iOS, make generic static libraries (Cocoa Touch Static Library)

Source: Internet
Author: User

There are several reasons why you might create a static library:

1. You want to share the tool code or the third-party plugin quickly to other people without having to copy a large number of files.
2. You want to get some general-purpose code under your control so that it can be repaired and upgraded.
3. you want to share the library with others, but don't want them to see your source code .

XCODE6 Create static library details (Cocoa Touch static libraries)

A. Create a static library file

Open Xcode, select file----> New---> Project. New project.

Select iOS----> Framework & Library---> Cocoa Touch Static Library.

Click Next. Create a project

Project Catalog:

Declaring a method in the header file StaticTest1.h

#import <Foundation/Foundation.h> @interface statictest1:nsobject-(void) teststaticlib; @end

  

Implementing Methods in STATICTEST1.M

#import "StaticTest1.h" @implementation statictest1-(void) teststaticlib{    NSLog (@ "This is a static library test");} @end

  

two. Create a common static library

A static library compiled from Xcode. A file is divided into iOS device real machine (ARMv7 arm64), IPhone5 (i386), iphone5s (x86_64) and other versions due to the different CPU architectures supported, and each version of. A files cannot be mixed. In order to solve this problem, we need to make a common static library file

use iOS Device-iPhone5->iphone5s to generate the libstatictest1.a file in the products directory (the file name changes from red to black to compile successfully), you must first compile the iOS Device's. A file, otherwise it may not compile

By right-clicking the. a file->show in Finder find the file libstatictest1.a, rename libstatictest1s.a,libstatictest15.a,libstatictest15s.a in turn, Names are free.

Execute a merge statement using the Terminal command line tool under Mac

lipo-create/users/outeki/desktop/libstatictest1s.a/users/outeki/desktop/libstatictest15s.a/users/outeki/ Desktop/libstatictest15.a-output/users/outeki/desktop/test.a
File path can be placed at will, I put the files on the desktop convenient to write the merge statement, note that three file paths are separated by a space,/users/outeki/desktop/test.a both the generated generic static library file

If you do not know the system supported by each. A file, you can view the specific information by lipo-info the command line

Lipo-info/users/outeki/desktop/libstatictest1s.a

  

If you need to separate common static libraries into versions of static libraries, you can refer to the article http://www.cnblogs.com/wengzilin/p/3800856.html

three. Using a static library

New iOS project and create Lib folder to add header files StaticTest1.h and test.a files to the project

Import the header file in viewcontroller.m to use the method defined in the header file.

#import "ViewController.h" #import "StaticTest1.h" @interface Viewcontroller () @end @implementation Viewcontroller            -(void) viewdidload {    [super viewdidload];        StaticTest1 *test = [[StaticTest1 alloc] init];    [Test teststaticlib];    } @end

  

Test output:

Four. Knowledge Supplement

The introduction to this static library basically ends, if you want to customize the static library header file, you can create a new. h header file in the Static library project and replace the default. h header file configured in copy file with it.

Add Public.h header File

Introducing a header file that can be exposed in public.h

#import "StaticTest1.h"

  

Modify common interface files in configuration file build phases->copy file (Xcode5 was previously the Copy Header)

Command+r execute the project to get the corresponding. h and. A Files

How to use workspace to manage static library source code and native app codes will be discussed in the next article

Create and use static libraries in iOS, make generic static libraries (Cocoa Touch Static Library)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.