将dylib包装成framework动态库 - ShenYj/ShenYj.github.io GitHub Wiki

包装一个framework动态库

先参考下标准 framework 的目录结构, 这里以 RxSwift 为例

.

为了更接近于真实效果,接下来模仿这种目录结构手动生成一个 framework的动态库

代码

  • 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
    
  • 目录结构

    这里为了效果更加逼真,头文件放在了一个专门的Headers 文件夹中

    ├── Headers
    │   └── TestExample.h
    ├── TestExample.m
    └── build.sh

脚本

  • 准备脚本

    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
    
    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 \
    -all_load \
    TestExample.o -o TestExample

    为了更接近于真实 framework 的效果,去掉了前面的lib前缀,去掉了.dylib后缀
    按照之前的写法 TestExample.o -o libTestExample.dylib

    • 执行完脚本,就创建动态库

      目录结构:

      .
      ├── Headers
      │   └── TestExample.h
      ├── TestExample
      ├── TestExample.m
      ├── TestExample.o
      └── build.sh

包装成framework

  • 创建完动态库后,再把整个文件夹改成.framework

    .
    └── TestExample.framework
        ├── Headers
        │   └── TestExample.h
        ├── TestExample
        └── TestExample.m

    .o和脚本隐藏了, 不是重点

这样就手动包装出了一个framework 动态库

⚠️ **GitHub.com Fallback** ⚠️