编译链接动态库

# 编译test.m -> test.o
clang -target x86_64-apple-macos12.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk \
-I./AFNetworking \
-c test.m -o test.o

# 链接test.m -> AFNetworking动态库
clang -target x86_64-apple-macos12.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk \
-L./AFNetworking \
-lAFNetworking \
test.o -o test

# 调试
➜  链接动态库AFN file test
test: Mach-O 64-bit executable x86_64
➜  链接动态库AFN lldb
(lldb) file test
Current executable set to '/Users/joe.cheng/Video/动态库/上课代码/链接动态库AFN/test' (x86_64).
(lldb) r


# 动态库原理
# 编译test.m -> test.o
clang -target x86_64-apple-macos12.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk \
-I./dylib \
-c test.m -o test.o

# 编译TestExample.m -> TestExample.o
clang -target x86_64-apple-macos12.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk \
-c TestExample.m -o TestExample.o

# 链接TestExample.o -> libTestExample.dylib
## 1. 方式1
clang -dynamiclib  \
-target x86_64-apple-macos12.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk \
TestExample.o -o libTestExample.dylib



## 2.1 方式2(官方)
libtool -static -arch_only x86_64 TestExample.o -o libTestExample.a

# -lsystem -framework Foundation 指定系统库
# mach-o 添加image path,否则image not found

## 2.2 方式2(官方)
ld -dylib -arch x86_64 \
-macosx_version_min 12.1 \
-syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk \
-lsystem -framework Foundation \
-ObjC \
-install_name  /Users/joe.cheng/Video/动态库/上课代码/动态库原理/dylib/libTestExample.dylib \
libTestExample.a -o libTestExample.dylib

## ld -dylib -arch x86_64 -macosx_version_min 12.1 libTestExample.a -o libTestExample.dylib
## 2.2 会引起image not found
ld -dylib -arch x86_64 \
-macosx_version_min 12.1 \
-syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk \
-lsystem -framework Foundation \
-ObjC \
libTestExample.a -o libTestExample.dylib



# 链接test.o -> libTestExample.dylib
clang -target x86_64-apple-macos12.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk \
-L./dylib \
-lTestExample \
test.o -o test




你可能感兴趣的:(编译链接动态库)