两种静态库:.a和.framework 的区别
库是.o文件的合集
常见的库文件格式:
.a: 常见的静态库
.dylib: 动态库
.framework: framework实际上是一种打包方式,将库的二进制文件、头文件、有关的资源文件打包在一起。有静态库和动态库2种:静态库framework = header+.a+签名+资源文件, 动态库=header + .dylib + 签名 + 资源文件。
.xcframework: 2018年推出的,针对不同架构的制作剥离不同的库文件
库(L ibrary):-段编译好的二进制代码,加上头文件就可以供别人使用。
库的是使用场景:
1.某些代码需要给别人使用,但是我们不希望别人看到源码,就需要以,库的形式进行封装,只暴露出头文件。
2.对于某些不会进行大的改动的代码,我们]想减少编译的时间,就可以把它打包成库,因为库是已经编译好的二进制了,编译的时候只需要Link一下,不会浪费编译时间。
如何查看一个库是静态库还是动态库
cd到需查看的库文件目录里,使用命令: file + 库名称, 如下得出AFNetworking是一个静态库
Macintosh:静态库原理 johnson$ cd /Users/johnson/Downloads/静态库/上课代码/链接 静态库AFNetworking/AFNetworking
Macintosh:AFNetworking johnson$ file libAFNetworking.a
libAFNetworking.a: current ar archive
将test.m文件生成test.o目标文件
Macintosh:链接静态库AFNetworking johnson$ clang -x objective-c \
> -target x86_64-apple-macos10.14.6 \
> -fobjc-arc \
> -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.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/TestExample.framework/Headers
*/
查看库里的头文件列表
ar -t + 库名称
Macintosh:AFNetworking johnson$ 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
使用clang命令生成.o目标文件
clang 是一个 C、C++ 和 Objective-C 编译器,它包含 prepro- 处理、解析、优化、代码生成、汇编和链接。
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
ar命令参数:
`ar`压缩目标文件,并对其进行编号和索引,形成静态库。同时也可以解压缩静态库,查看有哪些目标文件:
ar -rc a.a a.o
-r: 像a.a添加or替换文件
-c: 不输出任何信息
-t: 列出包含的目标文件
当前静态库原理文件夹下面有test.m文件, 及StaticLibrary文件夹里面的TestExample.h,TestExample.m文件
test.m,TestExample.h,TestExample.m文件内容分别如下
#import <Foundation/Foundation.h>
//眼 mudule -> .h
// .h -> 二进制
#import "TestExample.h"
int main(){
NSLog(@"testApp----");
TestExample *manager = [TestExample new];
[manager lg_test: nil];
return 0;
}
#import <Foundation/Foundation.h>
@interface TestExample : NSObject
- (void)lg_test:(_Nullable id)e;
@end
#import "TestExample.h"
@implementation TestExample
- (void)lg_test:(_Nullable id)e {
NSLog(@"TestExample----");
}
@end
test.o链接libTestExample.a生成test可执行文件
①test.m 生成test.o目标文件, TestExample.m生成 TestExample.o目标文件
//生成test.o文件
clang -x objective-c \
-target x86_64-apple-macos10.14.6 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk \
-I./StaticLibrary \
-c test.m -o test.o
//生成TestExample.o文件
clang -x objective-c \
-target x86_64-apple-macos10.14.6 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk \
-c TestExample.m -o TestExample.o
/**
test.o链接libTestExample.a生成test可执行文件
-L./StaticLibrary 在当前目录的子目录StaticLibrary查找需要的库文件
-lTestExample 链接的名称为libTestExample/TestExample的动态库或者静态库
查找规则:先找lib+<library_name>的动态库,找不到,再去找lib+<library_name>的静态库,还找不到,就报错
*/
TestExample添加.dylib后缀,然后再删除.dylib后缀, TestExample.dylib = TestExample
② test.o链接libTestExample.a生成test可执行文件
clang -target x86_64-apple-macos10.14.6 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk \
-L./StaticLibrary \
-lTestExample \
test.o -o test
/**
test.o链接libTestExample.a生成test可执行文件
-L./StaticLibrary 在当前目录的子目录StaticLibrary查找需要的库文件
-lTestExample 链接的名称为libTestExample/TestExample的动态库或者静态库
查找规则:先找lib+<library_name>的动态库,找不到,再去找lib+<library_name>的静态库,还找不到,就报错
-syslibroot: 系统库文件的目录
*/
③在终端执行test可执行文件,查看链接是否成功 **
在终端执行lldb**命令进入lldb模式下,在执行 file test 查看文件类型, 然后执行 r 命令运行test可执行文件, 输入内容文件如下,
Macintosh:静态库原理 johnson$ lldb
(lldb) file test
Current executable set to 'test' (x86_64).
(lldb) r
Process 3972 launched: '/Users/johnson/Downloads/静态库/上课代码/静态库原理/test' (x86_64)
2021-12-30 14:51:42.718508+0800 test[3972:1076896] testApp----
2021-12-30 14:51:42.718756+0800 test[3972:1076896] TestExample----
Process 3972 exited with status = 0 (0x00000000)
制作静态库
.a静态库
ar -rc 将.o 文件打包为.a 文件
Macintosh:StaticLibrary johnson$ ar -rc libTestExample.a TestExample.o
/**
`ar`压缩目标文件,并对其进行编号和索引,形成静态库。同时也可以解压缩静态库,查看有哪些目标文件:
ar -rc a.a a.o
-r: 像a.a添加or替换文件
-c: 不输出任何信息
-t: 列出包含的目标文件
*/
.framework静态库
编译好的二进制文件,Headers放入以.framwork结尾的文件夹中
test.o 链接TestExample.framework生成test可执行文件
//生成test.o目标文件, -I./Frameworks/TestExample.framework/Headers \ 指定需要使用的TestExample.h目录
clang -target x86_64-apple-macos10.14.6 \
> -fobjc-arc \
> -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk \
> -I./Frameworks/TestExample.framework/Headers \
> -c test.m -o test.o
//生成test二进制执行文件
clang -target x86_64-apple-macos10.14.6 \
> -fobjc-arc \
> -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk \
> -F./Frameworks \
> -framework TestExample \
> test.o -o test
//在lldb模式下验证是否成
Macintosh:静态库与Framework johnson$ lldb
(lldb) file test
Current executable set to 'test' (x86_64).
(lldb) r
Process 6190 launched: '/Users/johnson/Downloads/静态库/上课代码/静态库与Framework/test' (x86_64)
2021-12-30 20:07:56.742017+0800 test[6190:1352737] testApp----
Process 6190 exited with status = 0 (0x00000000)
shell脚本
build.sh文件内容如下:
#定义变量SYSROOT、FILE_NAME、HEADER_SEARCH_PATH。 使用时直接:$变量名, 或者${变量名},{}外还可以拼接字符
SYSROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk
FILE_NAME=test
HEADER_SEARCH_PATH=./StaticLibrary
echo "-----开始编译test.m"
#一行shell脚本多个参数使用'\'来添加多个shell参数, 没有'\'表示一行命令结束
clang -x objective-c \
-target x86_64-apple-macos10.15 \
-fobjc-arc \
-isysroot $SYSROOT \
-I${HEADER_SEARCH_PATH} \
-c ${FILE_NAME}.m -o ${FILE_NAME}.o
echo "-----开始进入StaticLibrary"
#pushd进入到某个目录下,须与popd一起使用, 等同于cd, pushd的是临时, cd进入后就是新路劲无法返回
pushd ./StaticLibrary
clang -x objective-c \
-target x86_64-apple-macos10.15 \
-fobjc-arc \
-isysroot $SYSROOT \
-c TestExample.m -o TestExample.o
ar -rc libTestExample.a TestExample.o
echo "-----开始退出StaticLibrary"
popd
echo "-----开始test.o to test EXEC"
clang -target x86_64-apple-macos10.15 \
-fobjc-arc \
-isysroot $SYSROOT \
-L./StaticLibrary \
-lTestExample \
${FILE_NAME}.o -o ${FILE_NAME}
执行build.sh shell文件, 在shell文件所在目录下 ./shellfilename.sh即可。
如果提示zsh: permission denied: . fbuild. sh,没有执行权限, 使用chmod +x ./shellfilename.sh
chmod +x ./build.sh
执行build.sh 成功输出如下
Macintosh:静态库原理 johnson$ ./build.sh
-----开始编译test.m
-----开始进入StaticLibrary
~/Downloads/静态库/上课代码/静态库原理/StaticLibrary ~/Downloads/静态库/上课代码/静态库原理
-----开始退出StaticLibrary
~/Downloads/静态库/上课代码/静态库原理
-----开始test.o to test EXEC
pushd popd 与cd 区别
cd: 修改当前pwd目录栈,修改了就无法返回
pushd: 新建一个临时目录栈
popd:使用popd直接返回之前的目录栈
objdump 查看二进制文件里代码的使用
objdump --macho -d test
#import <Foundation/Foundation.h>
//眼 mudule -> .h
// .h -> 二进制
#import "TestExample.h"
int main(){
NSLog(@"testApp----");
// TestExample *manager = [TestExample new];
// [manager lg_test: nil];
return 0;
}
执行objdump --macho -d test 命令,输出如下
#import <Foundation/Foundation.h>
//眼 mudule -> .h
// .h -> 二进制
#import "TestExample.h"
int main(){
NSLog(@"testApp----");
TestExample *manager = [TestExample new];
[manager lg_test: nil];
return 0;
}
dead strip 代码剥离
使用workspace来联调framework,将.app project 和 .framework project放在同一个workspace里进行调试,编译APP同时也编译framework。
创建xcworkspace: file -> seve as workspace
分类是在在运行时动态加载的, 所以在含有分类的库文件在编译时没问题,但在运行时会找不到分类方法。比如下面的报错:
2022-01-07 15:55:40.224374+0800 LGApp[35583:14201692] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[LGOneObject lg_test_category]: unrecognized selector sent to instance 0x600002834890'
通常我们在遇到这个问题时百度告诉我们的就是设置buildsetting -> other linker flags为 -Objc, -all_load 等
此时我们也可以通过配置xcconfig文件来解决:
// -all_load 全部加载,不剥离代码
// -ObjC 只加载oc代码,其他剥离
// -force_load :那些静态库 -》dead strip 这个库不剥离
LGSTATIC_FRAMEWORK_PATH=${BUILD_DIR}/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)/LGStaticFramework.framework/LGStaticFramework
OTHER_LDFLAGS=-Xlinker -force_load $LGSTATIC_FRAMEWORK_PATH
// clang -Xlinker -all_load -> ld
dead strip :
dead strip 是链接器提供的优化方式,移除没有被入口点使用或者导出的代码。
dead strip 主要针对静态库链接过程中来进行四代码剥离,有4种方式: -noall_load、 -all_load 、-ObjeC 、-force_load <file>, 静态库中默认使用的是-noall_load进行编码。
使用 man ld 查看
man ld
然后 /dead_strip 得到如下
-dead_strip
Remove functions and data that are unreachable by the entry point or exported symbols.
// 移除没有被入口点使用或者导出的代码。
test.m文件内容如下
#import <Foundation/Foundation.h>
//眼 mudule -> .h
// .h -> 二进制
//#import "TestExample.h"
void global_function(){
}
int main(){
NSLog(@"testApp----");
// global_function();
// TestExample *manager = [TestExample new];
// [manager lg_test: nil];
return 0;
}
static void static_function(){
}
build.文件内容如下
#test 二进制可执行文件链接一个TestExample静态库
#定义变量SYSROOT、FILE_NAME、HEADER_SEARCH_PATH。 使用时直接:$变量名, 或者${变量名}
SYSROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk
FILE_NAME=test
HEADER_SEARCH_PATH=./StaticLibrary
echo "-----开始编译test.m"
#一行shell脚本多个参数使用'\'来添加多个shell参数, 没有'\'表示一行命令结束
: '
①将test.m 文件编译为test.o 文件
1、使用-x 指定编译文件语言类型为OC
2、生成的是X86_64_macOS架构的代码
3、使用ARC
4、isysroot 指定使用SDK的路径
5、-I 在指定目录寻找头文件 header search path
6、-c 生成目标头文件 -o 输出文件
'
clang -x objective-c \
-target x86_64-apple-macos10.15 \
-fobjc-arc \
-isysroot $SYSROOT \
-I${HEADER_SEARCH_PATH} \
-c ${FILE_NAME}.m -o ${FILE_NAME}.o
echo "-----开始进入StaticLibrary"
#pushd进入到某个目录下,须与popd一起使用, 等同于cd, pushd的是临时, cd进入后就是新路劲无法返回
: '
② 进入到./StaticLibrary文件目录下将TestExample.m 文件编译为TestExample.o文件, 然后将TestExample.o文件压缩为TestExample.a 静态库
1、使用-x 指定编译文件语言类型为OC
2、生成的是X86_64_macOS架构的代码
3、使用ARC
4、isysroot 指定使用SDK的路径
5、-c 生成目标头文件 -o 输出文件
6、使用ar -rc 将.o文件压缩为.a 静态库
'
pushd ./StaticLibrary
clang -x objective-c \
-target x86_64-apple-macos10.15 \
-fobjc-arc \
-isysroot $SYSROOT \
-c TestExample.m -o TestExample.o
ar -rc libTestExample.a TestExample.o
echo "-----开始退出StaticLibrary"
popd
<<'COMMENT'
③使用test.o文件生成一个链接TestExample静态库的test二进制可执行文件
1、生成x86_64_macOS架构代码
2、使用arc
3、使用SDK的路劲
4、静态库链接方式
5、指定链接库所在位置
6、指定链接库的名称
7、.o生成生成macho二进制可执行文件
通过-L 指定所链接静态的的位置
通过-l 指定所链接静态库名称
通过test.o 得到一个链接TestExample静态库的test可执行文件
COMMENT
echo "-----开始test.o to test EXEC"
clang -target x86_64-apple-macos10.15 \
-fobjc-arc \
-isysroot $SYSROOT \
-Xlinker -dead_strip \
-L./StaticLibrary \
-lTestExample \
${FILE_NAME}.o -o ${FILE_NAME}
静态库编译时是默认不链接的,当第③步命令内容如下时,
clang -target x86_64-apple-macos10.15 \
-fobjc-arc \
-isysroot $SYSROOT \
-L./StaticLibrary \
-lTestExample \
${FILE_NAME}.o -o ${FILE_NAME}
当第③步命令内容如下, 链接内容为-all_load,添加参数 -Xlinker -all_load
clang -target x86_64-apple-macos10.15 \
-fobjc-arc \
-isysroot $SYSROOT \
-Xlinker -all_load \
-L./StaticLibrary \
-lTestExample \
${FILE_NAME}.o -o ${FILE_NAME}
当第三部分命令内容如下, 链接参数为dead strip,-Xlinker -dead_strip,
clang -target x86_64-apple-macos10.15 \
-fobjc-arc \
-isysroot $SYSROOT \
-Xlinker -dead_strip \
-L./StaticLibrary \
-lTestExample \
${FILE_NAME}.o -o ${FILE_NAME}
dead strip:没有被导出符号使用的代码就会被移除
比如:global_function();这个函数没有被调动,如果打开注释,调用此函数, 那么结果如下
函数符号被什么调用
clang -target x86_64-apple-macos10.15 \
-fobjc-arc \
-isysroot $SYSROOT \
-Xlinker -dead_strip \
-Xlinker -all_load \
-Xlinker -why_live -Xlinker global_function \
-L./StaticLibrary \
-lTestExample \
${FILE_NAME}.o -o ${FILE_NAME}