xcode 如何在Xcode中查看静态库的内容?

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

how to view content of static library in Xcode?

iosxcodestatic-libraries

提问by zak

Thank you in advance. I have a static library, say libpics.a. I want to see its contents, such as the code of that library. My static library has one .h fileand one .a file, i can see content of .h file, there is only one method, but i can't see the content of .a file. After some search i can just find that, .a filecontains the coding part of or implementation of .h file'smethod. I am new to iOSdevelopment, the code in that .a file, i want to extract it, and use it.

先感谢您。我有一个static library,比如说 libpics.a。我想看看它的内容,比如那个库的代码。我的静态库有一个.h file和一个.a file,我可以看到 的内容.h file,只有一种方法,但是我看不到 的内容.a file。经过一番搜索,我可以找到它,.a file包含方法的编码部分或实现.h file's。我是iOS开发新手,其中的代码.a file,我想提取它并使用它。

I tried searching about how to open static library, but most of time i got search related to how to create static library and how to use it etc. But i just want to open static library file and just want to see the code in it's implementation file.

我尝试搜索有关如何打开静态库的信息,但大部分时间我都在搜索与如何创建静态库以及如何使用它等相关的内容。但我只想打开静态库文件,只想查看其实现中的代码文件。

I read something about nmand artool, but i don't understand that where to apply that code.

我读了一些关于nmar工具的东西,但我不明白在哪里应用该代码。

something like this

像这样的东西

nm -C libschnoeck.a | less

or

或者

ar -t libsamplerate.a

after installing command line tool, i wrote
ar -x phpFramework.a
code in terminal as per suggestion by Владимир Водолазкий. i got below lines..

安装命令行工具后,我 根据 Владимир Водолазкий 的建议在终端中编写了
ar -x phpFramework.a
代码。我得到了以下几行..

ar: phpFramework.a is a fat file (use libtool(1) or lipo(1) and ar(1) on it)
ar: phpFramework.a: Inappropriate file type or format

回答by Vladimir Vodolazkiy

You cannot see source code inside static library, just due to there are NO source codes there. Static Library in IOS like in any other Unix-like system contains set of compiled procedures/functions/methods.

您无法在静态库中看到源代码,因为那里没有源代码。IOS 中的静态库就像在任何其他类 Unix 系统中一样,包含一组已编译的过程/函数/方法。

Just take a close look to the Xcode log when ordinary project is building. You can find that first, *.m files are compiled into *.o format - it is actually binary format (which is different when source file is compiled for use in Simulator or on native device). Then these *.o files are linked into application. (Please do not blame me for this simplistic explanation %-))

在构建普通项目时,只需仔细查看Xcode日志即可。你会发现,首先,*.m 文件被编译成 *.o 格式——它实际上是二进制格式(当源文件被编译用于模拟器或本机设备时,这是不同的)。然后将这些 *.o 文件链接到应用程序中。(请不要因为这个简单的解释而责怪我 %-))

In fact static library is just a set of such precompiled *.o files. It is shipped by developer/owner to save your time on compilation or/and protect source code from modification. So you can only use it with the help of external calls, which are documented in .h files or you can extract separate modules (.o) from there and link it into your application "manually".

实际上静态库只是一组这样的预编译 *.o 文件。它由开发人员/所有者提供,以节省您的编译时间或/和保护源代码不被修改。因此,您只能在外部调用的帮助下使用它,这些调用记录在.h 文件中,或者您可以从那里提取单独的模块 (.o) 并将其“手动”链接到您的应用程序中。

回答by user151019

The code used to create the library is compiled into object files that are linked into the .a file. The .a file does not contain code and you can't get readable code from the .a file.

用于创建库的代码被编译成链接到 .a 文件的目标文件。.a 文件不包含代码,您无法从 .a 文件中获取可读代码。

However to use the library you do not need the code, just include the library in your Xcode project as per the Xcode documentationand #import the headers into your code so that the compiler knows what is in the libraries.

但是,要使用该库,您不需要代码,只需根据Xcode 文档将该库包含在您的 Xcode 项目中,并将标头 #import 到您的代码中,以便编译器知道库中的内容。

During the link phase of your project the linker will look at the object code generated from your code and the find unresolved symbols which it will then look for in the library and only pull in the objects from the library that are needed. (One benefit of static over dynamic libraries)

在项目的链接阶段,链接器将查看从您的代码生成的目标代码并找到未解析的符号,然后它将在库中查找并仅从库中提取需要的对象。(静态库优于动态库的一个好处)

nm will list the symbols that have been defined in the library and which your code can call.

nm 将列出已在库中定义且您的代码可以调用的符号。