静态库 .a的原理与创建

921 阅读3分钟

查看符号表命令

  • man nm 命令
  • q 退出
  • 搜索关键字 /-
  • n向下查找 N向上查找 如
  • /-p 查看-p 命令
  • nm --help

什么是库

  • 库:是一段编译好的二进制代码,加上头文件就可以提供给别人使用
  • 某些代码需要给别人使用,但是我们不希望被人看到源码,就需要以库的形式进行封装,只暴露出头文件。
  • 对于某些不会进行大的改动的代码,我们想减少编译的时间,就可以把它打包成库,因为库底已经编译好的二进制,编译的时候只需要Link一下,就不会浪费编译时间。

常见的库文件格式

  • .a :常见的静态库
  • .dylib : 动态库
  • .framework : 静态库与动态库结合
  • .xcframework :2018针对不同架构的库

研究AFNetworking

    1. 下载AFNetworking
    1. cd 进入这个文件
    1. file libAFNetworking.a
    1. 输入内容是 tab 可以补齐历史输入的命令
    1. 是一个文档格式
    1. ar -- creat and maintain library archives
    1. ar -t libAFNetworking.a
    • 静态库 .a 文件其实是 .o文件的合集

创建静态库

    1. man clang
    • clang - the Clang C,C++, and Objective-C compiler
  • 2.将 .m->.o
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 test.m -o test.o



/**
    将test.m编译成test.o:
    1. 使用OC
    2. 生成的是X86_64_macOS架构的代码
        Big Sur是:x86_64-apple-macos11.1,之前是:x86_64-apple-macos10.15
    3. 使用ARC
    4. 使用的SDK的路径在:
        Big Sur是:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk
        之前是:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk
 */

测试一下

  • 1.建立一个OC到桌面
  • 2.复制mian.m 到任意一个文件夹改名为 test.m
  • 3.进入这个文件夹
  • 4.终端输入这个命令
  • 5.多了一个.o文件
  • 6.sdk的路径 显示包内容后可以往后查找到

当你的项目引用了其他的库

  • 1.这里引入了AFNetworking 其他的库
  • 2.这样就报错了
  • 3.添加AFNetworking
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.o


/**
    将test.m编译成test.o:
    1. 使用OC
    2. 生成的是X86_64_macOS架构的代码
        Big Sur是:x86_64-apple-macos11.1,之前是:x86_64-apple-macos10.15
    3. 使用ARC
    4. 使用的SDK的路径在:
        Big Sur是:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk
        之前是:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk
    5. 用到的其他库的头文件地址在./Frameworks
 */

  • 4.生成了一个.o 文件
  • 5.当还有其他多个的其他的库 可以继续 -I./XXX

练习自己写一个库

  • 1.在练习文件夹里面在建一个文件夹 StaticLibrary
  • 2.创建一个 .h .m 写一个简单的方法
  • 3.外面的 test.m 引入这个TestExample
  • 4.通过命令先给StaticLibrary 里面生成一个.o 文件
    • cd 进入StaticLibrary文件
      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 TestExample.m -o TestExample.o 
      
    • 直接将.o 文件的后缀修改为.dylib 前缀加上 lib
    • 再将.dylib 删除
    • 老系统可以直接改成 .a
  • 5.外面的test.m 也变成 .o
    • cd .. 回车 : 回到目标文件的上层文件
      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.o
      ```
      
    
    1. 生成一个test.o 后把这个.o 变成可执行文件
 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 test
/**
 clang命令参数:
     -x: 指定编译文件语言类型
     -g: 生成调试信息
     -c: 生成目标文件,只运行preprocess,compile,assemble,不链接
     -o: 输出文件
     -isysroot: 使用的SDK路径
     1. -I<directory> 在指定目录寻找头文件 header search path
     2. -L<dir> 指定库文件路径(.a\.dylib库文件) library search path
     3. -l<library_name> 指定链接的库文件名称(.a\.dylib库文件)other link flags -lAFNetworking
     -F<directory> 在指定目录寻找framework framework search path
     -framework <framework_name> 指定链接的framework名称 other link flags -framework AFNetworking
 */
 
 /**
    test.o链接libTestExample.a生成test可执行文件
    -L./StaticLibrary 在当前目录的子目录StaticLibrary查找需要的库文件
    -lTestExample 链接的名称为libTestExample/TestExample的动态库或者静态库
    查找规则:先找lib+<library_name>的动态库,找不到,再去找lib+<library_name>的静态库,还找不到,就报错
 */
  • 7.运行这个test
    • lldb
    • file test
    • r
  • 总结:静态库就是一个 .o 文件的合集

创建一个 .a

/**
    libtool -static -o <OUTPUT NAME> <LIBRARY_1> <LIBRARY_2>
 */
  • 将之前的TestExample.o 变成 .a
libtool \
-static \
-o \
libTest.a \ 
> /Users/xsj/Documents/Swift_Practice/强化学习/静态库/静态库原理1/StaticLibrary/TestExample.o \
  • 如果有很多的.o 文件可以 \回车 后继续添加

  • ar -t 查看一下