Pod 创建私有库之引用三方Framework和libs的配置方法

使用pod管理库已经用了3年,当然也做过私有库,但在做私有库的时候有些东西并没有弄得非常清楚,最近项目需要用到将自己的工程打成Framework并将这个Framework做成pod来供其他人去引用,鼓捣了大概几个小时弄了一个例子,下面和大家分享一下过程和结果。这个Pod工程我以网易云信的SDK作为导入三方包为例,目的是为了演示如果做pod库引用三方frameworks以及libraries。

1、首先我们需要把自己需要打出来的framework包导出来,这个过程非常简单而且网上很多例子我就不细说了,我也曾写过一篇文章来说明这个过程,IOS打包动态库以及使用的过程和注意事项 如果有需要可以点开学习一下

2、第二步在github上新添加一个库这个相信大家都操作过我就不细说,然后将创建好的库clone下来,然后我们可以开心的进行下一步了。

3、下一步就是在clone下来的工程目录创建Pod工程,说是工程其实是做Pod引用相关,首先创建podspec文件和将需要的三方frameworks和libraries拷贝到podspec文件同级目录(当然也可以放到其新建的工程里其他目录下但配置的时候需要找相对podspec文件的相对路径),创建方法如下


 
 
pod spec create 组件名称
当执行完以上命令后在你当前目录会出现扩展名为podspec的配置文件,我使用的名称为PrivatePod2018,所以生成的文件名称是PrivatePod.podspec。 生成的PrivatePod.podspec中其实就是原始的配置选项,如下:
#
#  Be sure to run `pod spec lint PrivatePod2018.podspec' to ensure this is a
#  valid spec and to remove all comments including this before submitting the spec.
#
#  To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html
#  To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/
#

Pod::Spec.new do |s|

  # ―――  Spec Metadata  ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  These will help people to find your library, and whilst it
  #  can feel like a chore to fill in it's definitely to your advantage. The
  #  summary should be tweet-length, and the description more in depth.
  #

  s.name         = "PrivatePod2018"
  s.version      = "0.0.1"
  s.summary      = "A short description of PrivatePod2018."

  # This description is used to generate tags and improve search results.
  #   * Think: What does it do? Why did you write it? What is the focus?
  #   * Try to keep it short, snappy and to the point.
  #   * Write the description between the DESC delimiters below.
  #   * Finally, don't worry about the indent, CocoaPods strips it!
  s.description  = <<-DESC
                   DESC

  s.homepage     = "http://EXAMPLE/PrivatePod2018"
  # s.screenshots  = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"


  # ―――  Spec License  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Licensing your code is important. See http://choosealicense.com for more info.
  #  CocoaPods will detect a license file if there is a named LICENSE*
  #  Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'.
  #

  s.license      = "MIT (example)"
  # s.license      = { :type => "MIT", :file => "FILE_LICENSE" }


  # ――― Author Metadata  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Specify the authors of the library, with email addresses. Email addresses
  #  of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also
  #  accepts just a name if you'd rather not provide an email address.
  #
  #  Specify a social_media_url where others can refer to, for example a twitter
  #  profile URL.
  #

  s.author             = { "mingmingsuper" => "mingmingsuper@gmail.com" }
  # Or just: s.author    = "mingmingsuper"
  # s.authors            = { "mingmingsuper" => "mingmingsuper@gmail.com" }
  # s.social_media_url   = "http://twitter.com/mingmingsuper"

  # ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  If this Pod runs only on iOS or OS X, then specify the platform and
  #  the deployment target. You can optionally include the target after the platform.
  #

  # s.platform     = :ios
  # s.platform     = :ios, "5.0"

  #  When using multiple platforms
  # s.ios.deployment_target = "5.0"
  # s.osx.deployment_target = "10.7"
  # s.watchos.deployment_target = "2.0"
  # s.tvos.deployment_target = "9.0"


  # ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Specify the location from where the source should be retrieved.
  #  Supports git, hg, bzr, svn and HTTP.
  #

  s.source       = { :git => "http://EXAMPLE/PrivatePod2018.git", :tag => "#{s.version}" }


  # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  CocoaPods is smart about how it includes source code. For source files
  #  giving a folder will include any swift, h, m, mm, c & cpp files.
  #  For header files it will include any header in the folder.
  #  Not including the public_header_files will make all headers public.
  #

  s.source_files  = "Classes", "Classes/**/*.{h,m}"
  s.exclude_files = "Classes/Exclude"

  # s.public_header_files = "Classes/**/*.h"


  # ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  A list of resources included with the Pod. These are copied into the
  #  target bundle with a build phase script. Anything else will be cleaned.
  #  You can preserve files from being cleaned, please don't preserve
  #  non-essential files like tests, examples and documentation.
  #

  # s.resource  = "icon.png"
  # s.resources = "Resources/*.png"

  # s.preserve_paths = "FilesToSave", "MoreFilesToSave"


  # ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Link your library with frameworks, or libraries. Libraries do not include
  #  the lib prefix of their name.
  #

  # s.framework  = "SomeFramework"
  # s.frameworks = "SomeFramework", "AnotherFramework"

  # s.library   = "iconv"
  # s.libraries = "iconv", "xml2"


  # ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  If your library depends on compiler flags you can set them in the xcconfig hash
  #  where they will only apply to your library. If you depend on other Podspecs
  #  you can include multiple dependencies to ensure it works.

  # s.requires_arc = true

  # s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
  # s.dependency "JSONKit", "~> 1.4"

end

生成这个文件后你会发现文件中都是默认配置,然后我们需要详细配置这个文件,主要需要配置的内容如下: - s.name 指这个库的模块名称 - s.version 库的版本号,刚开始创建为0.0.1默认可以不用改 - s.summary 简要描述,用于简述这个库的主要功能 - s.description 项目的详细描述 - s.platform 这个是配置平台 - s.homepage 项目主页 - s.license 这个相信大家都知道我就不解释了一般MIT就行 - s.author 这个是配置作者的信息,可以使姓名加邮箱,也可以只姓名或只邮箱 - s.source 这个一定得正确配置,一般都是放到github上用github上项目的地址,我选用的地址是ssh方式如git@github.com:mingmingsuper/PrivatePod.git,你可能会问为啥你定义的名是PrivatePod2018为啥这里是PrivatePod呢,那是因为PrivatePod已经被人占用了
注意私有库的模块名称最后以podspec的name配置为准和项目创建的目录名称关系不大
  • s.source_file 这个是配置pod工程中的源文件,如果是Objective-C写的项目可以配置成类似"Classes//*.{h,m}"这样的,如果是swift写的可以配置成"Classes//*.swift",再如果是混编可以配置成"Classes//*.{h,m}","Classes//*.swift"这样的,一次类推如果还有c++混编大家自己去试吧和以上说的写法一样,以此类推。

  • s.public_header_files 需要暴露出去的头文件配置

针对s.frameworks和s.libraries以及s.vendored_libraries和s.vendored_frameworks这里先不进行粗略说明因为这篇文章本意是重点介绍这几个配置。


重要配置如下


由于我们做的这个私有pod需要引用外部的framework以及libraries所以我们需要配置vendored_libraries和vendored_frameworks
1)我们先简单说明一下framewordks,这个配置选项是我们配置的依赖的系统库,例如我们需要引用MobileCoreServices等系统库那么配置如下
s.frameworks = "MobileCoreServices", "SystemConfiguration", 'AVFoundation', 'CoreTelephony', 'CoreMedia', 'AudioToolbox', 'VideoToolbox'
2) s.libraries道理一样我们是配置系统提供的一些库,比如我们要引用libz.tbd等,我们需要如下配置写法
s.libraries = "c++", "sqlite3.0", "z"
你可能要问为什么libz.tbd写成z,很简单这是一个规则要去除lib和后边的.tbd,如果遇到配置系统library就需要这么简写,记住就行了。
以上是配置系统库引用的写法不做过多解释也没有太多注意的地方,很好理解。
3) s.vendored_libraries 由于网易云信的库引用了很多的.a文件所以我们需要通过这个配置将这些文件引用到我们pod工程中,写法有两种第一种是写成以逗号分隔的形式例如:'libaacplus.a','libcrypto.a',... 还有一种是简写为通用形式以 .a匹配所有.a文件,所以我们采用.a这种方式,这样不容易拼错也节省时间。 4)s.vendored_frameworks和s.vendored_libraries类似写法也很类似,我们同样采用简写形式
注意以上3和4是配置引用三方库的写法,这两点可以着重看一下。需要注意的是我把所有的framework和library都放到了项目的根目录所以没有加目录直接写文件名称,如果您将文件放到了固定其他目录必须制定完整路径才可以用,路径指相对于podspec文件的路径

4、以上就介绍完podspec文件里的配置了,下面我们来说库的提交到最后提交到cocoapods上。 - 第一步先提交到github上,然后打tag
git add .
git commit -m "add files"
git push
git tag -a 0.0.1 -m "new version"
git push --tags

  • 第二部需要把工程推到cocoapods的trunk上,首先需要注册
pod trunk register 'your email address' 'your name'
接下来就可以把工程推到trunk上了
pod trunk push PrivatePod.podspec
执行这个命令需要一点时间耐性等着就行,如果成功会有像以下的输出
[!] Found podspec `PrivatePod2018.podspec`
Updating spec repo `master`

CocoaPods 1.4.0 is available.
To update use: `sudo gem install cocoapods`

For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.4.0

Validating podspec
 -> PrivatePod2018 (0.0.3)

Updating spec repo `master`

CocoaPods 1.4.0 is available.
To update use: `sudo gem install cocoapods`

For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.4.0


--------------------------------------------------------------------------------
 
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
以下是一个简单的例子,演示了如何在Qt中创建和使用动态。 动态工程代码(mylib.pro): ``` TEMPLATE = lib CONFIG += dll QT += core gui HEADERS += mylib_global.h \ myclass.h SOURCES += myclass.cpp \ mylib_global.cpp TARGET = mylib DESTDIR = ../bin ``` mylib_global.h文件: ```cpp #ifndef MYLIB_GLOBAL_H #define MYLIB_GLOBAL_H #include <QtCore/qglobal.h> #if defined(MYLIB_LIBRARY) # define MYLIB_EXPORT Q_DECL_EXPORT #else # define MYLIB_EXPORT Q_DECL_IMPORT #endif #endif // MYLIB_GLOBAL_H ``` myclass.h文件: ```cpp #ifndef MYCLASS_H #define MYCLASS_H #include "mylib_global.h" #include <QObject> class MYLIB_EXPORT MyClass : public QObject { Q_OBJECT public: explicit MyClass(QObject *parent = nullptr); Q_INVOKABLE void doSomething(); }; #endif // MYCLASS_H ``` myclass.cpp文件: ```cpp #include "myclass.h" #include <QDebug> MyClass::MyClass(QObject *parent) : QObject(parent) { } void MyClass::doSomething() { qDebug() << "MyClass::doSomething() called."; } ``` 使用动态的程序代码: ```cpp #include <QCoreApplication> #include <QLibrary> #include "myclass.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 加载动态 QLibrary myLib("mylib.dll"); if(!myLib.load()) { qDebug() << "Failed to load library."; } // 获取MyClass类的指针 MyClass *myClass = (MyClass*)myLib.instance(); if(myClass == nullptr) { qDebug() << "Failed to get MyClass instance."; } else { // 调用MyClass的函数 myClass->doSomething(); } return a.exec(); } ``` 需要注意的是,在编译使用动态的程序时,需要在.pro文件中添加LIBS += -lmylib或者LIBS += ../lib/mylib.dll(取决于动态文件的名称和路径)。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值