1.動態庫原理
1.1自己生成動態庫(失敗)
我們還是用在靜態庫中的TestExample案例先生成動態庫
為了方便我們使用創建build.sh,來使用腳本
clang -target x86_64-apple-macos11.0.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk \
-I./dylib \
-c test.m -o test.o
pushd ./dylib
echo "編譯TestExample.m --- TestExample.o"
clang -target x86_64-apple-macos11.0.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk \
-c TestExample.m -o TestExample.o
clang -dynamiclib \
-target x86_64-apple-macos11.0.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk \
TestExample.o -o libTestExample.dylib
echo "編譯TestExample.m --- libTestExample.dylib"
clang -target x86_64-apple-macos11.0.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk \
-L./dylib \
-lTestExample \
test.o -o test
運行后報錯還是和上面一樣 dyld: Library not loaded: libTestExample.dylib
1.2.先生成靜態庫再鏈接成動態庫
//把我們的o文件變成a文件。也可以使用ar -rc a.a a.o
libtool -static -arch_only x86_64 TestExample.o -o libTestExample.a
ld -dylib -arch x86_64 \
-macosx_version_min 11.0 \
-syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk \
-lsystem -framework Foundation \
-all_load \ //如果沒有這個參數,鏈接器會默認not all load
libTestExample.a -o libTestExample.dylib
運行之后還是報錯
Process 25266 launched: '/Users/MacW/Desktop/loginlearn/強化版/強化班-4-動態庫/上課代碼/動態庫原理/test' (x86_64)
dyld: Library not loaded: libTestExample.dylib
Referenced from: /Users/MacW/Desktop/loginlearn/強化版/強化班-4-動態庫/上課代碼/動態庫原理/test
Reason: image not found
錯誤顯示庫沒有加載進來
我們查看test加載所需要的動態庫
otool -l test | grep 'DYLIB' -A 5
//日志如下
cmd LC_LOAD_DYLIB
cmdsize 48
name libTestExample.dylib (offset 24)
time stamp 2 Thu Jan 1 08:00:02 1970
current version 0.0.0
compatibility version 0.0.0
--
cmd LC_LOAD_DYLIB
cmdsize 96
name /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (offset 24)
time stamp 2 Thu Jan 1 08:00:02 1970
current version 1770.106.0
compatibility version 300.0.0
--
cmd LC_LOAD_DYLIB
cmdsize 56
name /usr/lib/libobjc.A.dylib (offset 24)
time stamp 2 Thu Jan 1 08:00:02 1970
current version 228.0.0
compatibility version 1.0.0
--
cmd LC_LOAD_DYLIB
cmdsize 56
name /usr/lib/libSystem.B.dylib (offset 24)
time stamp 2 Thu Jan 1 08:00:02 1970
current version 1292.0.0
compatibility version 1.0.0
--
cmd LC_LOAD_DYLIB
cmdsize 104
name /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (offset 24)
time stamp 2 Thu Jan 1 08:00:02 1970
current version 1770.106.0
compatibility version 150.0.0
我們看到我們自己創建的庫 name libTestExample.dylib (offset 24),而其他的動態庫的name確實庫的路徑。
我們的動態庫能夠自己保存自己的路徑
otool -l ./dylib/libTestExample.dylib | grep 'ID' -A 5
//-A是向下尋找5行,-B是向上查找
cmd LC_ID_DYLIB
cmdsize 48
name libTestExample.dylib (offset 24)
time stamp 1 Thu Jan 1 08:00:01 1970
current version 0.0.0
compatibility version 0.0.0
--
cmd LC_UUID
cmdsize 24
uuid F26CDB5B-F71B-3658-86BE-C93CBE279ABE
Load command 9
cmd LC_BUILD_VERSION
cmdsize 32
我們看到name 還是 libTestExample.dylib
我們可以使用install_name_tool去給動態庫添加路徑
man install_name_tool
NAME
install_name_tool - change dynamic shared library install names
install_name_tool就是改變動態的路徑。
給動態路添加路徑
install_name_tool -id /Users/MacW/Desktop/loginlearn/強化版/強化班-4-動態庫/上課代碼/動態庫原理/dylib/libTestExample.dylib libTestExample.dylib
重新查看動態庫的LC_ID_DYLIB
otool -l libTestExample.dylib | grep 'ID' -A 5
cmd LC_ID_DYLIB
cmdsize 144
name /Users/MacW/Desktop/loginlearn/強化版/強化班-4-動態庫/上課代碼/動態庫原理/dylib/libTestExample.dylib (offset 24)
time stamp 1611538070 Mon Jan 25 09:27:50 2021
current version 0.0.0
compatibility version 0.0.0
--
cmd LC_UUID
cmdsize 24
uuid F26CDB5B-F71B-3658-86BE-C93CBE279ABE
Load command 9
cmd LC_BUILD_VERSION
cmdsize 32
然后重新編譯和鏈接我們的test文件運行成功
我們通過install_name_tool,重新給編譯過的動態庫加的路徑,其實ld提供給我們一個參數可以直接加的install_name
所以我們初期的sh腳本如下
clang -target x86_64-apple-macos11.0.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk \
-I./dylib \
-c test.m -o test.o
pushd ./dylib
echo "編譯TestExample.m --- TestExample.o"
clang -target x86_64-apple-macos11.0.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk \
-c TestExample.m -o TestExample.o
echo "編譯TestExample.o --- libTestExample.a"
# Xcode->靜態庫
libtool -static -arch_only x86_64 TestExample.o -o libTestExample.a
# -dynamiclib: 動態庫
# dylib 最終鏈接產物 -》
ld -dylib -arch x86_64 \
-macosx_version_min 11.0 \
-syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk \
-lsystem -framework Foundation \
-install_name /Users/MacW/Desktop/loginlearn/強化版/強化班-4-動態庫/上課代碼/動態庫原理/dylib/libTestExample.dylib \
-all_load \
libTestExample.a -o libTestExample.dylib
popd
echo "鏈接libTestExample.dylib -- test EXEC"
clang -target x86_64-apple-macos11.0.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk \
-L./dylib \
-lTestExample \
test.o -o test
運行沒問題
2.動態庫復用性
我們在上面指定的路徑是我們的絕對路徑,如果動態庫換了位置,很明顯還是會出現之前出現的問題, Reason: image not found
2.1@rpath解決絕對路徑的困擾
@rpath
Runpath search Paths
dyld搜索路徑
運行時@rpath
指示dyld
按順序搜索路徑列表,以找到動態庫。
@rpath
保存一個或多個路徑的變量
所以我們修改腳本
-install_name @rpath/dylib/libTestExample.dylib
運行還是報錯,為什么呢?我們可以查看test文件是否有rpath的設置
2.2 rpath
otool -l test | grep 'RPATH' -A 5
我們通過install_name_tool給test添加rpath路徑
install_name_tool -add_rpath /Users/MacW/Desktop/loginlearn/強化版/強 化班-4-動態庫/上課代碼/動態庫原理 test
正常運行。但是add_rpath也是我們手動添加的,沒有可復用性。
2.3.executable_path和loader_path
@executable_path
:表示可執行程序所在的目錄,解析為可執行文件的絕對路徑。
@loader_path
:表示被加載的Mach-O
所在的目錄,每次加載時,都可能 被設置為不同的路徑,由上層指定。
install_name_tool -rpath /Users/MacW/Desktop/loginlearn/強化版/強化班-4-動態庫/上課代碼/動態庫原理 @executable_path test
運行test成功
3.動態庫鏈接動態庫
test.m文件使用Frameworks中的TestExample.framework
在TestExample.framework庫中使用TestExampleLog.framework
3.1先編譯并鏈接TestExampleLog.framework
clang -target x86_64-apple-macos11.0.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk \
-I./Headers \
-c TestExampleLog.m -o TestExampleLog.o
clang -dynamiclib \
-target x86_64-apple-macos11.0.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk \
-Xlinker -install_name -Xlinker @rpath/TestExampleLog.framework/TestExampleLog \
TestExampleLog.o -o TestExampleLog
install_name是提供給外部需要鏈接TestExampleLog的路徑。
通過查看otool -l TestExampleLog | grep 'ID' -A 5能看出外部需要鏈接的路徑
cmd LC_ID_DYLIB
cmdsize 72
name @rpath/TestExampleLog.framework/TestExampleLog (offset 24)
time stamp 1 Thu Jan 1 08:00:01 1970
current version 0.0.0
compatibility version 0.0.0
--
cmd LC_UUID
cmdsize 24
uuid 73F8F987-0378-346D-B014-890611B76871
Load command 9
cmd LC_BUILD_VERSION
cmdsize 32
3.2.再編譯鏈接TestExample.framework
clang -target x86_64-apple-macos11.0.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk \
-I./Headers \
-I./Frameworks/TestExampleLog.framework/Headers \
-c TestExample.m -o TestExample.o
clang -dynamiclib \
-target x86_64-apple-macos11.0.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk \
-Xlinker -install_name -Xlinker @rpath/TestExample.framework/TestExample \
-Xlinker -rpath -Xlinker @loader_path/Frameworks \
-F./Frameworks \
-framework TestExampleLog \
TestExample.o -o TestExample
echo "-------DYLIB---------"
otool -l TestExample | grep 'DYLIB' -A 5
echo "-------ID---------"
otool -l TestExample | grep 'ID' -A 5
install_name是提供給調用者加載的路徑
rpath是提供給調用TestExampleLog的初始路徑
3.3編譯并鏈接test
clang -target x86_64-apple-macos11.0.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk \
-I./Frameworks/TestExample.framework/Headers \
-c test.m -o test.o
clang \
-target x86_64-apple-macos11.0.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk \
-Xlinker -rpath -Xlinker @executable_path/Frameworks \
-F./Frameworks \
-framework TestExample \
test.o -o test
運行正常打印
3.4.test文件怎么使用TestExampleLog
如果我們想使用TestExampleLog,我們需要能再TestExample中導出符號中能看到TestExampleLog
objdump --macho --exports-trie TestExample
TestExample:
Exports trie:
0x000080C0 _OBJC_METACLASS_$_TestExample
0x000080E8 _OBJC_CLASS_$_TestExample
我們沒有看出有導出符號,所以我們無法在test使用TestExampleLog,我們需要使用reexport_framework
我們修改TestExample內的腳本
clang -dynamiclib \
-target x86_64-apple-macos11.0.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk \
-Xlinker -install_name -Xlinker @rpath/TestExample.framework/TestExample \
-Xlinker -rpath -Xlinker @loader_path/Frameworks \
-Xlinker -reexport_framework -Xlinker TestExampleLog \
-F./Frameworks \
-framework TestExampleLog \
TestExample.o -o TestExample
查看DYLIIB信息
otool -l TestExample | grep 'DYLIB' -A 5
cmd LC_ID_DYLIB
cmdsize 72
name @rpath/TestExample.framework/TestExample (offset 24)
time stamp 1 Thu Jan 1 08:00:01 1970
current version 0.0.0
compatibility version 0.0.0
--
cmd LC_REEXPORT_DYLIB
cmdsize 72
name @rpath/TestExampleLog.framework/TestExampleLog (offset 24)
time stamp 2 Thu Jan 1 08:00:02 1970
current version 0.0.0
compatibility version 0.0.0
可以知道TestExample重新加一個cmd,LC_REEXPORT_DYLIB
我們在test中導入頭文件
#import <Foundation/Foundation.h>
#import "TestExample.h"
#import "TestExampleLog.h"
int main(){
NSLog(@"testApp----");
TestExample *manager = [TestExample new];
[manager lg_test: nil];
TestExampleLog *log = [TestExampleLog new];
NSLog(@"testApp----%@",log);
return 0;
}
我們的腳本文件也需要-I頭文件,重新運行腳本,正常打印。