The most common library file formats in iOS are.a.dylib.framework. Xcframework. Today we will explore.a files, also known as static libraries.

. A file

View the. A file information

First of all, let’s look at the. A file. Let’s look at the. A information in AFNetWorking.

bel@beldeMacBook-Pro AFNetworking % file libAFNetworking.a
libAFNetworking.a: current ar archive
Copy the code

We can see that it is actually a document format

NAME AR -- Create and maintain Library archives SYNOPSIS AR -d [-TLSV] archive file... ar -m [-TLsv] archive file ... ar -m [-abiTLsv] position archive file ... ar -p [-TLsv] archive [file ...]  ar -q [-cTLsv] archive file ... ar -r [-cuTLsv] archive file ... ar -r [-abciuTLsv] position archive file ... ar -t [-TLsv] archive [file ...]  ar -x [-ouTLsv] archive [file ...]Copy the code

We can see that AR can create and modify archive files

Split into.o files

Let’s take apart the dot A file

bel@beldeMacBook-Pro AFNetworking % ar -t libAFNetworking.a 
__.SYMDEF
AFAutoPurgingImageCache.o
AFHTTPSessionManager.o
AFImageDownloader.o
AFNetworkActivityIndicatorManager.o
AFNetworking-dummy.o
AFNetworkReachabilityManager.o
AFSecurityPolicy.o
AFURLRequestSerialization.o
AFURLResponseSerialization.o
AFURLSessionManager.o
UIActivityIndicatorView+AFNetworking.o
UIButton+AFNetworking.o
UIImageView+AFNetworking.o
UIProgressView+AFNetworking.o
UIRefreshControl+AFNetworking.o
WKWebView+AFNetworking.o
Copy the code

From this we can see that.a files are a collection of.o files.

Linked static library

Let’s create a new folder, import AFNetWorking, and create a test.m file

In test.m, we add the following code

#import <Foundation/Foundation.h>
#import <AFNetworking.h>

int main(){
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSLog(@"testApp----%@", manager);
    return 0;
}

Copy the code

Generate object file

We compile test.m into an.o file.

Clang-x objective-c \ -target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ -i/AFNetworking \  -c test.m -o test.oCopy the code
  • -x: Specifies the language type.
  • -target: specifies the architecture.
  • -fobjc-arc: indicates the arc environment.
  • -isysroot: indicates the compiled SDK path.
  • -i: specifies the header file location.Corresponding to header Search path in Xcode.
  • -c xxx.m -o xx.o: Compiles the. M file to an. O file.

So we compile test.m into the test.o file.

Link object files to generate executables

We specify the language at compile time; we do not specify the language at link time

Clang-target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk \ -L./AFNetworking \ -lAFNetworking \ test.o -o testCopy the code
  • -l < dir > : specify the library file path (.a.dylib library file),Corresponds to the Library Search Path in Xcode
  • -l< libarayr_name>: specifies the linked library file name (.a.dylib library file).Corresponding to Other Link Flags in Xcode

Lib +< library_name> -l = static lib+< library_name> -l = static lib+< library_name>

The process of linking is to relocate the symbols in the relocation table to bind the real symbol address. The three elements of the link file are 1, the header location of the library file. 2. Library file path. 3. Library file name.

Static library principle

Let’s create a new file StaticLibrary

And create a new TestExample file, and in the.m file, we’re just going to simply print one character

@implementation TestExample

- (void)test:(_Nullable id)e {
    NSLog(@"TestExample----");
}
@end
Copy the code

1. Compile testexample. m as an.o file

Clang-x objective-c \ -target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - c the TestExample. M - o  TestExample.oCopy the code

2, we’re going toTestExample.oChange the name of theTestExample.dylibFinally, will.dylibTo get rid ofWe use the file command to view the current file

file libTestExample.dylib
libTestExample.dylib: Mach-O 64-bit object x86_64
Copy the code

Even if we change the name, it’s still an object file in itself. Now we’ll use the test.m file to link libTestExample to see if it works. If it does, we’ll verify from the side that the static library file is a collection of.o files.

We compile test.m into the test.o file

Clang-x objective-c \ -target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ -i/StaticLibrary \  > -c test.m -o test.oCopy the code

3, use test.o to link libTestExample

Clang-target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - l. / StaticLibrary \  -lTestExample \ test.o -o testCopy the code

Once the link is successful

4. Run the test executable file and enter LLDB in the terminal to go to the LLDB environment

5. Run in LLDB

(LLDB) file test Current executable set to '/Users/bel/Desktop/ test' (x86_64). (LLDB) R Process 99621 launched: '/Users/bel/Desktop/ Static Library /test' (x86_64) 2021-03-06 23:37:58.265213+0800 test[9961:13587736] testApp---- 2021-03-06 23:37:58.265760+0800 test[9961:13587736] TestExample---- Process 99621 exited with status = 0 (0x00000000) (LLDB)Copy the code

Our executable works fine

This means that our static library file link is successful, which means that the static library is a collection of.o files.

Static library merge

LibAFNetworking. A: Libsdwebimage. a: libAFNetworking. A: libAFNetworking.

Let’s take a look at the libtool command

man libtool
NAME
       libtool - create libraries
       ranlib - add or update the table of contents of archive libraries
Copy the code

This command is used to create library files or update a series of static library files

libtool \
-static \
-o \
> libAll.a \
> ./libAFNetworking.a \
> ./libSDWebImage.a  
Copy the code

This completes the merging of the.o files of the static library.

Static libraries and Framework

.framework is a set of.a files and headers for a static library file.

Let’s start by compiling test.m as an object file

Clang-x Objective-c-target x86_64-apple-macos11.1-fobjc-arc-isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1. The SDK -I./Frameworks/TestFramework.framework/Headers -c test.m -o test.oCopy the code

The framework is then linked

Clang-target x86_64-apple-macos11.1-fobjc-arc-isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK - F/Frameworks -framework TestExample test.o -o testCopy the code
  • -f < directory>: searches for the framework in the specified directory.
  • -framework < framework_name> Specifies the framework name of the link

The appendix

Summary of Clang directives used in this article

-x: specify the language type of the compiled file. -g: generate debugging information. -C: generate the target file, run preprocess, compile, assemble, no link. -l <dir> Specify the library file path (.a\.dylib library file) library search path 3. -l<library_name> Specify the linked library file name (.a\.dylib library file) other link flags -lafnetworking -f <directory> Find framework Framework search in the specified directory Path-framework <framework_name> Specifies the framework name of the link other Link flags - Framework AFNetworkingCopy the code

Lldb-related commands

File < executable > : loads the executable file into the LLDB r: executes the executable file.Copy the code