ar和libtool合并.a静态库

优质
小牛编辑
123浏览
2023-12-01

.a静态库合并

静态库是.o文件的合集, 静态库合并就是将静态库的.o合并, 可以实现的工具

  • ar, 静态库的查看、拆分、合并
  • libtool, xcode提供的静态库操作工具

libtool演示.a静态库合并

一、准备

  1. staticLib文件夹里准备好两个.a静态库

  2. 目录结构

     .
     ├── libAFNetworking.a
     └── libSDWebImage.a
    

二、合并

  1. 执行命令

     libtool \
     -static \
     -o \
     libMerge.a \
     libAFNetworking.a \
     libSDWebImage.a
    
    • 参数说明

      • libMerge.a是合并后生成静态库的名称
      • libAFNetworking.alibSDWebImage.a是将要被合并的两个静态库
    • 生成最终静态库

        .
        ├── libAFNetworking.a
        ├── libMerge.a
        └── libSDWebImage.a
      
  2. 利用ar来验证下静态库文件是否包含两个静态库的.o文件

     ar -t /Users/shenyj/Documents/CodeForTest/staticLibMerge/staticLib/libMerge.a
    

    输出结果

     ❯ ar -t /Users/shenyj/Documents/CodeForTest/staticLibMerge/staticLib/libMerge.a
     __.SYMDEF
     AFHTTPSessionManager.o
     AFNetworking-dummy.o
     AFNetworkReachabilityManager.o
     AFSecurityPolicy.o
     AFURLRequestSerialization.o
     AFURLResponseSerialization.o
     AFURLSessionManager.o
     NSBezierPath+SDRoundedCorners.o
     NSButton+WebCache.o
     NSData+ImageContentType.o
     NSImage+Compatibility.o
     SDAnimatedImage.o
     SDAnimatedImagePlayer.o
     SDAnimatedImageRep.o
     SDAnimatedImageView+WebCache.o
     SDAnimatedImageView.o
     SDAssociatedObject.o
     SDAsyncBlockOperation.o
     SDDeviceHelper.o
     SDDiskCache.o
     SDDisplayLink.o
     SDFileAttributeHelper.o
     SDGraphicsImageRenderer.o
     SDImageAPNGCoder.o
     SDImageAssetManager.o
     SDImageAWebPCoder.o
     SDImageCache.o
     SDImageCacheConfig.o
     SDImageCacheDefine.o
     SDImageCachesManager.o
     SDImageCachesManagerOperation.o
     SDImageCoder.o
     SDImageCoderHelper.o
     SDImageCodersManager.o
     SDImageFrame.o
     SDImageGIFCoder.o
     SDImageGraphics.o
     SDImageHEICCoder.o
     SDImageIOAnimatedCoder.o
     SDImageIOCoder.o
     SDImageLoader.o
     SDImageLoadersManager.o
     SDImageTransformer.o
     SDInternalMacros.o
     SDMemoryCache.o
     SDWeakProxy.o
     SDWebImage-dummy.o
     SDWebImageCacheKeyFilter.o
     SDWebImageCacheSerializer.o
     SDWebImageCompat.o
     SDWebImageDefine.o
     SDWebImageDownloader.o
     SDWebImageDownloaderConfig.o
     SDWebImageDownloaderDecryptor.o
     SDWebImageDownloaderOperation.o
     SDWebImageDownloaderRequestModifier.o
     SDWebImageDownloaderResponseModifier.o
     SDWebImageError.o
     SDWebImageIndicator.o
     SDWebImageManager.o
     SDWebImageOperation.o
     SDWebImageOptionsProcessor.o
     SDWebImagePrefetcher.o
     SDWebImageTransition.o
     UIButton+WebCache.o
     UIColor+SDHexString.o
     UIImage+ExtendedCacheData.o
     UIImage+ForceDecode.o
     UIImage+GIF.o
     UIImage+MemoryCacheCost.o
     UIImage+Metadata.o
     UIImage+MultiFormat.o
     UIImage+Transform.o
     UIImageView+HighlightedWebCache.o
     UIImageView+WebCache.o
     UIView+WebCache.o
     UIView+WebCacheOperation.o
    

ar演示.a静态库合并

  1. 准备两个.a静态库: libAFNetworking.alibSDWebImage.a

     .
     ├── libAFNetworking.a
     └── libSDWebImage.a
    
  2. 先将.a展开成.o文件, 利用x参数

    • 展开AFNetworking

        ar x /Users/shenyj/Documents/CodeForTest/lib/staticLibMerge/staticLib/libAFNetworking.a
      
    • 展开SDWebImage

        ar x /Users/shenyj/Documents/CodeForTest/lib/staticLibMerge/staticLib/libSDWebImage.a
      
  3. 利用ar合并

     ar -rc libMerge.a *.o
    

    生成新的.a静态库: libMerge.a

  4. 验证下合并是否成功

     ar -t /Users/shenyj/Documents/CodeForTest/lib/staticLibMerge/staticLib/libMerge.a
    

    会显示两个静态库的全部.o文件