将 .a 库文件类型与 XCode 集成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15884584/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:09:46  来源:igfitidea点击:

Integrating .a library file types with XCode

iosxcode

提问by Bryan Roberts

I am testing an image capture library that sent over their code library as a regular header file and a file ending in a "*.a" extension. I am not familiar with using a file in this format and can't seem to find the right "magic" to search for.

我正在测试一个图像捕获库,该库将其代码库作为常规头文件和以“*.a”扩展名结尾的文件发送。我不熟悉使用这种格式的文件,似乎找不到合适的“魔法”来搜索。

What does this file extension stand for?

这个文件扩展名代表什么?

What if any extra steps are needed to get it integrated with my XCode project?

如果需要任何额外的步骤将它与我的 XCode 项目集成怎么办?

回答by Thiago F. Alencar

Maybe a bit more of theory can get you in the right track so you'll know how to search next time:

也许多一点理论可以让你走上正轨,这样你下次就会知道如何搜索:

.a files are archives of object (.o) files. These object files are generally produced by an assembler, compiler, or other language translator; They contain machine code that is usually not directly executable, but are used by linker tools to generate an executable or yet another library by combining parts of these object files.

.a 文件是目标 (.o) 文件的存档。这些目标文件通常由汇编器、编译器或其他语言翻译器生成;它们包含通常不能直接执行的机器代码,但链接器工具使用它们通过组合这些目标文件的部分来生成可执行文件或另一个库。

Another important thing you should know is that since these files contain machine code, they must have been compiled for the correct architecture you're targeting for (ex.: armv7, i386, etc). That can be the reason why sometimes you will be able build your project for one device but not to another if you don't have the right set of files for the targeted platform (e.g. iPhone simulator vs. actual iPhone).

您应该知道的另一件重要事情是,由于这些文件包含机器代码,因此它们必须针对您所针对的正确体系结构进行编译(例如:armv7、i386 等)。这可能就是为什么有时如果您没有针对目标平台的正确文件集(例如 iPhone 模拟器与实际 iPhone),您将能够为一个设备构建您的项目,但不能为另一个设备构建您的项目。

It is also possible to have "universal binaries", which are files that in turn package together archives of object files (.a) for different architectures (e.g. using tools such as lipo), so that the same library file can be given to the linker for multiple architectures. Or, you may have a different linker configuration for each target (e.g. using conditional build settings in XCode). All of these files can be analyzed with certain tools (e.g. "file", "otool", "lipo -info", etc), and each has several parameters to play with. Debuggers will also check the symbols within these files for their own purposes.

也可能有“通用二进制文件”,这些文件依次将不同架构的目标文件 (.a) 的存档打包在一起(例如使用 lipo 之类的工具),以便可以将相同的库文件提供给多种架构的链接器。或者,您可能对每个目标都有不同的链接器配置(例如,在 XCode 中使用条件构建设置)。所有这些文件都可以使用某些工具(例如“file”、“otool”、“lipo -info”等)进行分析,并且每个文件都有几个参数可供使用。调试器也会出于自己的目的检查这些文件中的符号。

When you drag the '.a' file to your project's directory within Xcode, you can notice that it will automatically add this file to the list of "Link Binary With Libraries" items under your target's "Build Phases".

当您将“.a”文件拖到 Xcode 中的项目目录时,您会注意到它会自动将此文件添加到目标“构建阶段”下的“链接二进制与库”项目列表中。

Since the header files allows us to separate certain elements of a program's source code into reusable files, that commonly contain forward declarations of classes, subroutines, variables, and other identifiers that are needed for reference during the compilation step, it is common to have libraries provided as archives of compiled objects (.o) in ".a" files along with their respective headers. For instance, as soon as you include the headers files in your XCode project, the editor will provide auto-complete for these new functions.

由于头文件允许我们将程序源代码的某些元素分离成可重用的文件,这些文件通常包含类、子例程、变量和其他在编译步骤中需要引用的标识符的前向声明,因此通常有库以“.a”文件中的编译对象 (.o) 档案及其各自的头文件的形式提供。例如,只要您在 XCode 项目中包含头文件,编辑器就会为这些新功能提供自动完成功能。

Sometimes, you might also consider having the actual source code of the library instead of binaries inside your project as this * might * make debugging easier in case an unexpected behavior is happening inside that "library" package. Many times this is not an option though (when you have no access to the sources) when the binary distribution is made in purpose to hide implementation details (commercial libraries).

有时,您可能还会考虑在您的项目中使用库的实际源代码而不是二进制文件,因为这 * 可能 * 使调试更容易,以防在“库”包中发生意外行为。很多时候,当二进制分发旨在隐藏实现细节(商业库)时,这不是一个选项(当您无法访问源代码时)。

回答by epatel

.a stands for archive. It is also known as a static library. I believe you should be able just to drag it and the header files into Xcode. Xcode should pick up the right thing to do from its extension.

.a 代表存档。它也被称为静态库。我相信您应该能够将它和头文件拖到 Xcode 中。Xcode 应该从它的扩展中选择正确的事情去做。

Example, see appr. from 30 sec here http://memention.com/ac3dreader/usage/quickstart.html

示例,参见 appr。从这里开始 30 秒 http://memention.com/ac3dreader/usage/quickstart.html

Another example from Google Analytics, under Setup

另一个来自 Google Analytics 的示例,位于“设置”下

Drag GANTracker.h and libGoogleAnalytics.a from the SDK's Library directory into your new project.

将 GANTracker.h 和 libGoogleAnalytics.a 从 SDK 的库目录中拖到您的新项目中。

https://developers.google.com/analytics/devguides/collection/ios/devguide

https://developers.google.com/analytics/devguides/collection/ios/devguide