Using a static library in Xcode

Today I'd like to share some information on a topic that is dear to me: building static libraries with your Xcode projects.

I launched into testing something today and ran into issues I hadn't encountered under Xcode 3.  In the course of searching for the one right way to add static libraries to my project I found many forum and blog posts, all a little different from one another (and most from earlier this year).

Here are the steps for setting up my test project.

Set up your Workspace

First I created a static library project using Xcode's static library template (File...New...New Project...)

Next I created a workspace using Xcode (File...New...New Workspace). This is the preferred way of grouping related projects, and in this case I want to group my static library project with the project that relies on it.

Note that existing projects can be converted into workspaces (File...Save As Workspace...).

I created a new project called SimpleSearchDemo within the workspace. I then control-clicked under that project in the navigator to add my existing SimpleSearch static library .xcodeproj file.

A note: I repeatedly tried adding my static library project to this workspace but it kept appearing without the disclosure arrow.  After an hour or two I had a headache and restarted Xcode and tried again. It worked as expected. I still think about those hours sometimes. Collateral damage.

Edit your static library build settings

Next I selected the static library project to edit its build settings. I set it up to skip install and made sure the Copy Headers build phase included the headers. In particular, I added the public-facing header to the Public section.

Configure the project that uses the library

Once the static library was set up, it was time to configure the client project.

Link the static library with the project target; it should appear under 'Workspace' libraries.

Adjust the build settings so that the User Header Search Paths include the directory where the static library's headers are copied. I used the BUILT_PRODUCTS_DIR after reading this post. Both projects belong to the same Workspace, and thus share a common build directory.  Enable recursive searching of the user header search paths, and enable Always Search User Paths.

Finally, ensure the static library is built for archiving.

One other thing. I had issues with code completion working. I dragged the "public" header from the static library into the client project, andunchecked the 'Add to targets' checkbox.  Autocompletion started working (the header is right there, after all), but this seems unnecessary - let me know how you got around it.  Any other improvements are welcome.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值