how to view content of static library in Xcode?

11,809

Solution 1

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.

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 %-))

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".

Solution 2

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.

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

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.

Share:
11,809
zak
Author by

zak

Updated on June 04, 2022

Comments

  • zak
    zak about 2 years

    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 file and 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 file contains the coding part of or implementation of .h file's method. I am new to iOS development, the code in that .a file, i want to extract it, and use it.

    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 nm and ar tool, but i don't understand that where to apply that code.

    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: 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
    
  • zak
    zak over 10 years
    thank you for deep explanation. :) can you tell me how to extract separate modules from there? i don't want to use the same static library for my application, but i want to use some part of this static library, that's why i want to see the contents or modules of it.
  • zak
    zak over 10 years
    i don't want to use the same static library for my application, but i want to use some part of this static library, that's why i want to see the contents or modules of it.
  • Vladimir Vodolazkiy
    Vladimir Vodolazkiy over 10 years
    ar -t <libName> allows to list all modules in library, ar -x <libName> <moduleName> extracts module into separate file <moduleName>
  • zak
    zak over 10 years
    where i should write this line?
  • Vladimir Vodolazkiy
    Vladimir Vodolazkiy over 10 years
    In Terminal. This is is a regular Unix command. But usually you do not need to make this operations. Xcode allows to use static libraries, you don't need to extract modules from there :-)
  • zak
    zak over 10 years
    thank you.. i tried this in terminal but i got error. Aakashs-Mac-mini:~ aakash$ ar -t phpFramework.a -bash: ar: command not found
  • zak
    zak over 10 years
    i installed command line tool. tun i tried this code line in terminal, i got as below. aakash$ ar -x phpFramework.a ar: phpFramework.a: No such file or directory i tried to set path using cd command but it doesn't work.
  • Vladimir Vodolazkiy
    Vladimir Vodolazkiy over 10 years
    Xcode automatically extracts all modules which are used in your application as a calls to function in this library. Just add library to frameworks
  • Bhupesh Pant
    Bhupesh Pant about 10 years
    @Mark, I am not sure how it works for IOS but in c++, static library links to your program image completely. Which means that whether or not you are using the complete functionality you will have all the features of the library. Please correct me if I am wrong since this is my theoretical knowledge..
  • mmmmmm
    mmmmmm about 10 years
    @BhupeshPant No a static library when it is linked only supplies the code that is used (this is how the linker works so is independent of the language)
  • Bhupesh Pant
    Bhupesh Pant about 10 years
    oh I see... one more question, when you say that it why it produces language independant code, what do you mean by this? I need a tool like dependency walker through which I can actually peek inside the lib for all the exposed funcs. Do you know any?
  • mmmmmm
    mmmmmm about 10 years
    @BhupeshPant Tese should be separate questions however they are covered in most tutorials about program executable structure