静态库

2021-01-26  本文已影响0人  卟过尔尔

TestExample.h
TestExample.m

TestExample.m —> TestExample.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 TestExample.m -o TestExample.o

命令:
-x: 指定编译文件语言类型,使用OC
-target:指定架构,Big Sur是:x86_64-apple-macos11.1,之前是:x86_64-apple-macos10.15
-fobjc-arc:使用ARC
-isysroot: 使用的SDK路径
-c: 生成目标文件,只运行preprocess,compile,assemble,不链接
-o: 输出文件
\ : 终端换行符

TestExample.o —> libTestExample.a

#方法一
ar -rc libTestExample.a TestExample.o
#方法二 Xcode用法
libtool -static -arch_only x86_64 TestExample.o -o libTestExample.a

命令:
ar:压缩目标文件,并对其进行编号和索引,形成静态库。同时也可以解压缩静态库,查看有哪些目标文件:
-r: 像a.a添加or替换文件
-c: 不输出任何信息
-t: 列出包含的目标文件

test.m 链接一个静态库StaticLibrary(libTestExample.a)

image.png

test.m —> test.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 \
-I./StaticLibrary \
-c test.m -o test.o

命令:
-I: 在指定目录寻找头文件 (header search path)

test.o —> test.exec

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

命令:
-L: 指定库文件路径(.a.dylib库文件) library search path
-l: 指定链接的库文件名称(.a.dylib库文件)other link flags -lAFNetworking

查看效果

image.png
lldb
file test
r

Build.sh脚本执行

image.png
综合上述代码后,sh中的一部分代码:

pushd ./StaticLibrary (进入文件目录)
popd (返回

执行脚本

./build.sh

#zsh: permission denied: ./build.sh 报错后 需要增加可执行权限
chmod -x ./build.sh
#或者这样 u代表所有者
chmod u+x build.sh

test.m 链接一个Frameworks(TestExample.framework/Headers/TestExample.h)
TestExample.framework/TestExample文件是libTestExample.a去掉lib和.a

image.png

test.m —> test.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 \
-I./Frameworks/TestExample.framework/Headers \
-c test.m -o test.o

test.o —> test.exec

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

-F: 在指定目录寻找framework framework search path
-framework: 指定链接的framework名称 other link flags -framework AFNetworking

上一篇下一篇

猜你喜欢

热点阅读