記macOS Mojave下使用PB的一些經(jīng)驗(yàn)

介紹

介紹的話,不必多說了,網(wǎng)上很多內(nèi)容

安裝

  1. 安裝必要工具,電腦上已經(jīng)安裝的brew,通過brew install來安裝必要的工具,有autoconf,automake,libtool,protobuf,aclocal。期間可能會(huì)遇到報(bào)錯(cuò),說brew無法連接,這時(shí)需要輸入命令
sudo chown -R $(whoami) /usr/local
  1. 在項(xiàng)目主頁下有文檔說明,安裝說明依次輸入命令:
$ git clone https://github.com/protocolbuffers/protobuf.git
$ cd protobuf
$ git submodule update --init --recursive
$ ./autogen.sh
$ ./configure

進(jìn)行./configure 可能會(huì)報(bào)錯(cuò),不過別著急,先分析錯(cuò)誤信息

configure: error:ERROR: protobuf headers are required.You must either install protobuf from google,or if you have it installed in a custom locationyou must add '-Iincludedir' to CXXFLAGSand '-Llibdir' to LDFLAGS.If you did not specify a prefix when installingprotobuf, try'./configure CXXFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib'In some 64-bit environments, try LDFLAGS=-L/usr/local/lib64.

仔細(xì)看,不難發(fā)現(xiàn)終端給出了解決辦法,我想這應(yīng)該是跟系統(tǒng)是不是64位有關(guān)吧(個(gè)人猜測)。
如果

$ ./configure CXXFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib

運(yùn)行還是報(bào)錯(cuò),換成下面這條語句

 ./configure CXXFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib64

成功后,輸入

$ make
$ make install

沒什么意外的話,環(huán)境應(yīng)該建立起來了。

一鍵添加pb文件到Xcode項(xiàng)目里

每次都要pb文件有更新的話,都要手動(dòng)更新的話會(huì)煩不勝煩的,幸好在網(wǎng)上搜索了一下,發(fā)現(xiàn)幾篇文章有涉及到相關(guān)內(nèi)容。

  1. 自動(dòng)編譯PB文件,并添加到項(xiàng)目
  2. 使用代碼為 Xcode 工程添加文件
  3. ruby庫xcodeproj使用心得

參考上面幾篇文章,基本上實(shí)現(xiàn)了將pb文件一鍵添加到Xcode項(xiàng)目里,不過還有點(diǎn)缺憾,相關(guān)的.m 文件是非arc的,所以怎么實(shí)現(xiàn)在同一個(gè)ruby源文件里,實(shí)現(xiàn)add compiler flags呢?通過Google搜索,在github上找到了相關(guān)內(nèi)容。
我摘錄下來

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

require 'xcodeproj'

# Add compiler flags to "Build Phases > Compile Sources"
def add_compiler_flags(xcproj, flags)
  xcproj.targets.each do |target|
    target.source_build_phase.files_references.each do |fileref|
      # You can check fileref with fileref.path or fileref.parent
      # fileref.parent is PBXGroup or PBXProject

      # In this sample, we check if any parent group has "WinObjC/Frameworks" path
      add_flags = false
      r = fileref
      loop {
          if r.kind_of?(Xcodeproj::Project::Object::PBXProject) || r.source_tree != '<group>'
              break
              # 根據(jù)自己的項(xiàng)目修改path
          elsif r.path == 'WinObjC/Frameworks'
              add_flags = true
              break
          end
          r = r.parent
      }
      next unless add_flags
      fileref.build_files.each do |bf|
        # TODO: merge settings with original COMPILER_FLAGS
        bf.settings = {'COMPILER_FLAGS' => flags}
      end
    end
  end
end

def main
  //根據(jù)自己的內(nèi)容修改調(diào)用的代碼
  xcproj_path = ARGV.shift
  flags = ARGV.shift

  xcproj = Xcodeproj::Project.new(xcproj_path)
  xcproj.initialize_from_file

  add_compiler_flags(xcproj, flags)

  xcproj.save
end

main

至此折騰的差不多了。相關(guān)的文件,上面參考的文章都有相應(yīng)的內(nèi)容,如果需要更改的話,根據(jù)自己實(shí)際項(xiàng)目去修改即可。因?yàn)橄嚓P(guān)資料零零散散的,特此整理一下。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容