将静态库链接到 Xcode 4 中的 iOS 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6124523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linking a static library to an iOS project in Xcode 4
提问by Matt Hudson
I have a project (AQGridView
) that compiles to a static library, but I can't seem to add it to my project.
我有一个AQGridView
编译为静态库的项目 ( ),但我似乎无法将其添加到我的项目中。
Dragging in the project to my project creates a workspace, and if I try to link the libAQGridView.a
file from the DerivedData
directory it doesn't recognize it as a library. I'm not sure what I'm doing wrong.
将项目拖入我的项目会创建一个工作区,如果我尝试libAQGridView.a
从DerivedData
目录中链接文件,它不会将其识别为库。我不确定我做错了什么。
This is the AQGridView
project. Does anyone know specifically how to use it in an Xcode 4 project?
这就是AQGridView
项目。有谁知道如何在 Xcode 4 项目中使用它?
回答by Halle
I do this as follows:
我这样做如下:
- Drag in the static library project. If you have the static library project open in Xcode, close it now.
- Select the main project in the project navigator (the project I'm adding the static library to) and in the editor, under the header TARGETS in the left-hand column, select my main project's target and navigate to the Build Phases tab.
- Click the "+" for Target Dependencies and add the library icon target dependency from the added static library project.
- Click the "+" for Link Binary with Libraries and add the library icon that is under the folder "Workspace".
- It may also be necessary to enter a Header Search Path for the headers of the static library project if that is how the headers are linked in the static library project itself.
- 拖入静态库项目。如果您在 Xcode 中打开了静态库项目,请立即关闭它。
- 在项目导航器中选择主项目(我要将静态库添加到的项目)并在编辑器中,在左侧列中的标题 TARGETS 下,选择我的主项目的目标并导航到 Build Phases 选项卡。
- 单击 Target Dependencies 的“+”并从添加的静态库项目中添加库图标 target 依赖项。
- 单击 Link Binary with Libraries 的“+”并添加位于文件夹“Workspace”下的库图标。
- 如果静态库项目本身中的标头是这样链接的,则可能还需要为静态库项目的标头输入标头搜索路径。
If you don't see the static library project as nested under the main project in the main project's project navigator, the most likely reason for that is that the static library's own Xcode project is still open. Quit Xcode and open up the main project that has the nested static library project in it without opening up the original static library project itself, and you should see it appearing as a nested project in your main project.
如果在主项目的项目导航器中没有看到嵌套在主项目下的静态库项目,最可能的原因是静态库自己的 Xcode 项目仍处于打开状态。退出 Xcode 并打开其中包含嵌套静态库项目的主项目,而不打开原始静态库项目本身,您应该会看到它在主项目中显示为嵌套项目。
回答by Kazuki Sakamoto
Xcode menu > View > Utilities > File Inspector
Xcode 菜单 > 查看 > 实用工具 > 文件检查器
Select the static library file, and then set 'File Type' as 'Mach-O object code' in 'Identity and Type'.
选择静态库文件,然后在“身份和类型”中将“文件类型”设置为“Mach-O 目标代码”。
回答by Richard J. Ross III
Find your .a file in finder, and drag it into your project.
在 finder 中找到您的 .a 文件,并将其拖到您的项目中。
Select the 'copy items into destination group's folder (if needed)', and add your headers to your project.
选择“将项目复制到目标组的文件夹(如果需要)”,并将您的标题添加到您的项目中。
Now, XCode 4 should automatically link against that framework for you.
现在,XCode 4 应该会自动为您链接到该框架。
Here is what the library should look like in your project:
这是库在您的项目中的外观:
回答by Joey Gibson
Workspaces are supposed to make this easier, but I don't know that they do. The way I do it is create a workspace, add my main project, add my library project. Then go into the main project's build phases and add the library in the "Link binary with libraries" section and add the library. That shouldbe all that is necessary, at least that's my reading, but it isn't. What I do is go into Xcode preferences, then to the Source Trees pane. Add a source tree that points to your static library's headers, then go back to the build settings for your project, then to the Header Search Paths key, and enter ${foo} there, where "foo" is whatever you called the source tree.
工作区应该让这更容易,但我不知道他们这样做。我这样做的方法是创建一个工作区,添加我的主项目,添加我的库项目。然后进入主项目的构建阶段并在“Link binary with libraries”部分添加库并添加库。这应该是所有必要的,至少这是我的阅读,但事实并非如此。我所做的是进入 Xcode 首选项,然后进入 Source Trees 窗格。添加一个指向静态库头文件的源代码树,然后返回到项目的构建设置,然后返回到 Header Search Paths 键,并在那里输入 ${foo},其中“foo”就是你所说的源代码树.
That's what works for me, though I don't know if that's the best or easiest way to do it.
这就是对我有用的方法,尽管我不知道这是否是最好的或最简单的方法。
回答by Corin
Follow Apple's documentation.
遵循 Apple 的文档。
In brief:
简单来说:
- Link your target against the library.
- Add
-ObjC
to 'Other Linker Flags' (OTHER_LDFLAGS
) under the target's build settings.
- 将您的目标与库相关联。
- 添加
-ObjC
到OTHER_LDFLAGS
目标构建设置下的“其他链接器标志”( )。
Also, I needed to add the library to the scheme of my main project since the library was not visible in the target's dependencies.
此外,我需要将该库添加到我的主项目的方案中,因为该库在目标的依赖项中不可见。
回答by IZIordanov
The Halle's answer works for me with one addition:
哈雷的回答对我有用,还有一个补充:
- Check in Build Settingsof the static library project the Private( or Public) Headers Folder Pathvalue and copy it.
- Add copied value to the client project Build SettingsHeader Search Pathor User Header Search Pathdepending of the includedirective you are using
- 检查静态库项目的构建设置私有(或公共)头文件夹路径值并复制它。
- 将复制的值添加到客户端项目Build Settings Header Search Path或User Header Search Path取决于您使用的包含指令