JHHK

欢迎来到我的个人网站
行者常至 为者常成

静态库

目录

0301 man nm 与 common符号

1. 使用man命令
man nm
man objdump
man git

2. 使用--help
nm --help
objdump --help
git --help

0302 链接AFN生成目标文件

1 快捷键
command + k 清除终端屏幕
control + E  定位到尾部
control + W  向前清除一个单词


2
.a  静态库
.dylib 动态库
.framework 资源文件 + 静态库 / 动态库
.xcframework 不同的架构连接所需的framework


3 命令
//查看文件类型
file libAFNetworking.a

//验证libAFNetworking.a是.o文件的合集
ar -t libAFNetworking.a

//如果只有一个xxx.m文件
我们将xxx.m文件编译为xxx.o文件
将xxx.o文件直接修改文件名为libxxx.a,它就可以作为一个静态库使用
从另外一个角度证明,静态库就是.o文件的合集.



4 生成目标文件
man clang 查看clang命令
目标文件.o内有,重定位符号表,表内只需要标记哪些符号需要重定位
所以生成目标文件的过程,只需要知道libAFNetworking.a头文件即可.

0303 链接静态库生成可执行文件

1 符号表
.o文件1的重定位符号表
.o文件2的重定位符号表


2 静态库
静态库的本质是.o文件的合集


3 链接一个静态库

静态库的搜索路径
-L <dir>                Add directory to library search path

指定链接的库文件的名称
-l<library_name> 指定链接的库文件名称(.a/.dylib库文件)

链接后,所有的重定位符号表都会被放入一个统一的符号表中

0304 framework初探

1 静态库合并
就是将A静态库的.o文件和B静态库的.o文件放在一块,就是静态库合并.
libtool - create libraries
ranlib  - add or update the table of contents of archive libraries


2 头文件 
mudule -> .h
.h -> 二进制,不用每次都编译


3 framework
静态库:头文件 + .a    + 签名 + 资源文件
动态库:头文件 + dylib + 签名 + 资源文件

0305 framework

一 自己创建一个framework

TestExample.framework
    Headers(文件夹)
        TestExample.h
    TestExample(.a文件)

二 链接一个framework

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 test

0306 shell

1 sh文件格式
build.sh 

2 将指令放入build.sh文件

3 获取权限:chmod +X ./build.sh 

4 shell的基本使用

echo "------"           #打印
pushd ./StaticLibrary   #进入一个目录
popd                    #回到上一级目录
CustomVar = xxxx        #变量
$CustomVar              #变量使用
${CustomVar}.mm         #变量使用

0307 dead strip

一. 查看代码段
objdump --macho -d test


二.dead code strip

1.
embed 会将静态库拷贝到bundle包内

2.
dead code strip 是在链接阶段起作用
删除入口点或导出符号无法访问的函数和数据。

other link flag 是通过 clang 给我们的链接器传递参数的
-Xlinker 是告诉clang 我们是给ld传递参数的


3.
-all_load   Loads all members of static archive libraries.
-all_load 参数 告诉链接器不要脱掉任何代码,把所有的代码都加进去
这样我们就享受不到链接器对我们进行的优化,因为所有的代码都被加进去了.


4.-ObjC 只加载OC的
-ObjC       Loads all members of static archive libraries that implement an Objective-C class or category.


5. -force_load 指定加载哪些静态库
-force_load path_to_archive
Loads all members of the specified static archive library.  
Note: -all_load forces all members of all archives to be loaded.
This option allows you to target a specific archive.


6. 说明
other link flag 的 -all_load ,  -ObjC , -force_load 是对链接阶段的链接配置
dead code strip 也是作用在链接阶段,但跟other link flag是两回事.


7. 了解LTO

0308 下节课内容

动态库加载原理 -> dyld image not found 路径
实际开发.动态库与静态库的链接场景

行者常至,为者常成!





R
Valine - A simple comment system based on Leancloud.