Creating a Static Library with Xcode 4.6 or Higher

Creating a Static Library

Now you will create a static library.

  1. In Xcode, create a new project by selecting New image Project … from the Xcode File menu.
  2. In the New Project Assistant pane, create a Cocoa library by first selectingFramework & Library (under the Mac OS X section), and then selecting theCocoa Library icon in the upper-right pane (as shown in Figure B-8).

    9781430250500_AppB-08.jpg

    Figure B-8Creating a Cocoa Library

  3. The Project Options window is then displayed. Specify the Product Name asHelloLib, the Type drop-down list as Static (to create a static library), and select the check box to specify that the project will use Automatic Reference Counting for memory management. Do not select the check box for Include Unit Tests, and accept the defaults for the rest of the options.
  4. Now click Next to bring up the Project Options window. In this window, specifyHello for location of the project, select HelloSpace in the Add to: drop-down list (to add this library to the HelloSpace workspace), and then click the Createbutton.
  5. In the Xcode project navigator, observe that an empty HelloLib class is created. Select HelloLib.h and update the class interface as shown in Listing B-2.

    Listing B-2.  HelloLib Class Interface

    #import <Foundation/Foundation.h>
    
    @interface HelloLib : NSObject
    
    - (NSString *) greeting:(NSString *)salutation;
    
    @end
  6. The interface declares a single method named greeting: that takes an NSStringobject as its argument. Now select HelloLib.m and update the class implementation as shown in Listing B-3.

    Listing B-3.  HelloLib Class Implementation

    #import "HelloLib.h"
    
    @implementation HelloLib
    
    - (NSString *) greeting:(NSString *)salutation
    {
      return [NSString stringWithFormat:@"%@, World!", salutation];
    }
    
    @end

The method returns a greeting that includes the input parameter. This completes the implementation of the static library. Now you need to make the header publicly available to users of the library.

  1. In the project editor pane of the workspace window (under Targets), select theHelloLib target, and then the Build Phases tab.
  2. Delete the Copy Headers phase (if provided), and then add the Copy Files build phase using the Add Build Phase icon (as shown in Figure B-9).

    9781430250500_AppB-09.jpg

    Figure B-9Adding the Copy Files build phase

  3. Now disclose for the Copy Files phase by clicking the neighboring triangle. You will see a location for adding files at the bottom of the window (it’s labeled Add files here). From the project navigator, drag a copy of the HelloLib.h header into this area and release it. This will make this header file available to clients.
  4. In the Copy Files area, set the Subpath to include/${PRODUCT_NAME}, and select Products Directory in the Destination drop-down list. The workspace window should now look as shown in Figure B-10.

    9781430250500_AppB-10.jpg

    Figure B-10Updating the Copy Files build phase

    Note   The subpath setting will copy files into a folder named after the library (i.e., HelloLib), inside a folder named include, inside the workspace built products directory. This setting enables you to organize header files for different libraries by library name. Theinclude folder inside a build products directory is in the default header search path for applications.

  5. Finally, you should share the library, thereby making it available to any project using the workspace. In Xcode, select Scheme image Manage Schemes … from the Xcode Product menu. The Manage Schemes dialog will be displayed. Then locate the HelloLib scheme in this dialog and click the Shared check box.
  6. Now select the Build Settings tab. Use the Search tool to find the Skip Installsetting and set that to Yes; this prevents Xcode from creating a multiapplication bundle when projects that use this library are archived. This setting must be made for both the HelloLib project and HelloLib target.

With those steps completed, your static library is properly configured. Now let’s create the app.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值