How to Create a static library in xcode 4

Source: Internet
Author: User

From: http://blog.csdn.net/pjk1129/article/details/7255163

Recently, I am working on iOS development. I have a need to develop static libraries. iOS development only allows static libraries or frameworks. In xcode, no allow compilation is found, just like *. So on Android and DLL on Win32. However, the framework is probably similar to the implementation of dynamic libraries. However, no specific research has been conducted and further research will be conducted in the future.

The static library of this document is developed based on xcode4.2 and iOS sdk5.0. Xcode4 is quite different from the previous xcode3.

Below is a simple example of writing a static library and calling a static library.

Static library Compilation:

1. Create a static library project:

In xcode new, select framework & library under iOS, and there is a cocoa touch static library. Create a project such as print directly next.


2. Project Establishment

We add the simple "-(void) printa;" and "+ (void) printb;" methods. The implementation of this method is also very simple, in *. the implementation in the M file is an "nslog (@" ");", so that the compiled libstaticlib can be called by other IOs projects. Note that the current print-> iOS device



3. Preparations before Compilation

The release and DEBUG Versions need to be set.


4. OK, compile

We can find the compiled libstaticlib. A file under products.

Note: The value is red before Compilation: black after left Compilation: Right


 

5. OK

Right-click the file on the right and choose open in finder. At this time, we opened a folder with libprint.. Next we will write an app that calls libprint.. However, this app may encounter errors during compilation. By solving this error, we will describe some xcode compilation-related items. Here, we find this file by path under the command, and use this very important command to view the information of this Lib!

Open the terminal and write the following command: What is this? This is the path

The following are the paths on your Mac. Replace/users/pjk1129/library/developer/xcode/deriveddata/print-dgfkluumuexoxhcapzidtsmdgqcj/build/products/with your path.

CD/users/pjk1129/library/developer/xcode/deriveddata/print-dgfkluumuexoxhcapzidtsmdgqcj/build/products/

Manually open this path: The following information is displayed: release-iphoneos


This is for the real machine, so we should be able to use the static library encapsulated for the simulator and the real machine. This article introduces !!!!!


 

Then open the file CD/users/user/library/developer/xcode/deriveddata/uitab-dbvoszbzpavyglaboxgflvrnzfce/build/products/debug-iphoneos on the terminal.

Command: lipo-Info libprint. A. The displayed result is: arch is arm7. I will introduce it in detail below.

6. Create an application that calls libprint..

The application is simple. Let's add the *. h file of libprint. A just now. Then add the libprint. A we just copied in the Link binary with library of build phases of this project.



7. After the H and A files are inserted

We can call this libprint. A function in control.

# Import "astrodiskappdelegate. H"

# Import "print. H"

 

 

@ Implementation astrodiskappdelegate

 

@ Synthesize window = _ window;

 

-(Void) dealloc

{

[_ Window release];

[_ Viewcontroller release];

[Super dealloc];

}

 

-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions

{

// Call libprint.

[Print printb];

Print * print = [[[Print alloc] init] autorelease];

[Print printa];

 

Self. Window = [[[uiwindow alloc] initwithframe: [[uiscreen mainscreen] bounds] autorelease];

[Self. Window makekeyandvisible];

Return yes;

}

 

 

8. OK. Compile and run the application.

Compilation error. This is the key to this document. The main purpose is to introduce the compilation of static library-related things.



9. You can see errors such as undefine symbols for architecture i386.

In fact, this error is very simple because we used the libuitab compiled by mistake. A Lib: in the simulator, what we need is static lib compiled based on the i386 architecture. However, do you still remember what we mentioned earlier in the file arm6 arm7. This a actually runs the code on the ARM architecture of the iPhone. In fact, show in finder points to an iOS device library, not a library under the i386 simulator. So how to compile the i386 library? Have you seen the print> iOS device of the second image? Modify the iOS device to iphone5.0 simulator. Compile to compile the library under i386. At this time, the folder opened by show in finder is still the library under iOS device. We 'd better take a look at it in that directory. Here we use command to view the directory, as shown below: we can see a directory for iPhone OS and a simulator.


Then let's see what is the difference between this directory and the Directory shown in article 4th! There is also a libprint. A file.



10. Error Cause Analysis

Release-iphoneos contains library files compiled based on arm6 arm7. The release-iphonesimulator folder contains i386-based files.

11. Merge the. A file to create a general static library.

One of these two libraries is used for real machine running and the other is used for simulator running. In fact, we can use lipo to package these two files into a common a file. The command is as follows:

Replace/users/user/library/developer/xcode/deriveddata/uitab-dgfkluumuexoxhcapzidtsmdgqcj/build/products with your path.

Replace/users/user/desktop/libprint. A with the merged path.

Lipo-create/users/pjk1129/library/developer/xcode/deriveddata/print-dgfkluumuexoxhcapzidtsmdgqcj/build/products/release-iphonesimulator/libprint. a/users/pjk1129/library/developer/xcode/deriveddata/print-dgfkluumuexoxhcapzidtsmdgqcj/build/products/release-iphoneos/libprint. a-output/users/pjk1129/desktop/libprint. A

 

12. Check the information of the packaged library file.

Run the following command: lipo-info/users/pjk1129/desktop/libprint..

You can see the following information:


As you can see above, it meets the i386 and arm7. Now, the static library supports armv7 i386.


13. OK. After the above steps

After this general library is generated, we will call this library to replace it and compile it successfully. The running result is as follows:




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.