链接动态库时指定路径 - ShenYj/ShenYj.github.io GitHub Wiki
链接framework动态库
中大部分的篇幅用于介绍动态库链接及常见问题分析,并通过 install_name_tool
设置动态库路径
这里强调的是链接过程直接设置路径
还是沿用 将dylib包装成framework动态库 里面的代码, 经过这一操作,我们已经有了一个 framework
的结构, 在这个结构下直接一次性生成动态库,并在链接时设置路径
-
代码
TestExample.h 、TestExample.m
-
TestExample.h
#import <Foundation/Foundation.h> @interface TestExample : NSObject - (void)lg_test:(_Nullable id)e; @end
-
TestExample.m
#import "TestExample.h" #import "TestExampleLog.h" @implementation TestExample - (void)lg_test:(_Nullable id)e { NSLog(@"TestExample----"); } @end
-
-
目录结构
. ├── Frameworks │ └── TestExample.framework │ ├── Headers │ │ └── TestExample.h │ ├── TestExample.m │ └── build.sh ├── build.sh └── test.m
-
在此基础上,调整下脚本
clang -target x86_64-apple-macos12.1 \ -fobjc-arc \ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk \ -I./Headers \ -c TestExample.m -o TestExample.o clang -dynamiclib \ -target x86_64-apple-macos12.1 \ -fobjc-arc \ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk \ -Xlinker -install_name -Xlinker @rpath/Frameworks/TestExample.framework \ TestExample.o -o TestExample
-
查看其
Load Command
验证路径是否被设置成功❯ otool -l TestExample | grep 'LC_ID_DYLIB' -A 3 cmd LC_ID_DYLIB cmdsize 64 name @rpath/Frameworks/TestExample.framework (offset 24) time stamp 1 Thu Jan 1 08:00:01 1970