轉自:http://www.th7.cn/Program/IOS/201506/484001.shtml
首先是怎么安裝Protobuf。 來自https://github.com/alexeyxo/protobuf-objc的文檔。
打開終端!
brew -v
查看你的mac里面有沒有裝brew。brew是mac os里面,類似于ubuntu的apt-get的功能,都可以直接在終端輸入命令然后安裝程序。-v自然就是版本version的意思
ruby -e $(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)
這一句半懂不懂,大概就是利用curl工具訪問那個url,然后在ruby環境下載安裝brew
建議先去Homebrew官網找最新的下載地址
brew install automake
brew install libtool
brew install protobuf
就是利用brew下載安裝了。protobuf就是我們想要的,另外兩個是依賴庫
git clone https://github.com/alexeyxo/protobuf-objc.git
./build.sh
從github下載protobuf-objc這個工程,build腳本里面做的是編譯。
我建議不要用 ./build.sh ,我安裝過程中發現未知錯誤最終沒有進行下去。哎,好失敗。懂腳本的朋友可以嘗試下。
到此,我們先得感謝 http://www.2cto.com/kf/201503/382440.html的文章作者。點開鏈接的朋友會發現,這都什么嗎,明顯照抄人家的。。。
我只能說,該作者前半部分解釋的非常好,我是超越不了了,只能完全借用了。其實說白了,就是懶。言歸正傳:
當我們 git clone https://github.com/alexeyxo/protobuf-objc.git 完成后,
cd ~/protobuf-objc
./autogen.sh
./configure
~/protobuf-objc其實就是剛剛clone的文件目錄
進行./configure 可能會報錯,不過別著急,先分析錯誤信息
configure: error:
ERROR: protobuf headers are required.
You must either install protobuf from google,
or if you have it installed in a custom location
you must add '-Iincludedir' to CXXFLAGS
and '-Llibdir' to LDFLAGS.
If you did not specify a prefix when installing
protobuf, try
'./configure CXXFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib'
In some 64-bit environments, try LDFLAGS=-L/usr/local/lib64.
仔細看,不難發現終端給出了解決辦法,我想這應該是跟系統是不是64位有關吧(個人猜測)。
./configure CXXFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib
運行通過后,
make
make install
最終生成的插件名字為protoc-gen-objc,會被安裝到/usr/local/bin/目錄下。
你可以
cd /usr/local/bin/
ls -a
按照我的方法,肯定能看見protoc-gen-objc。
一切準備就緒,我們來測試下。
在桌面創建一個 ProtoBuf的文件夾。然后
cd ~/Desktop/ProtoBuf
touch person.proto
vi person.proto
就按ProtocolBuffer的語法規則簡單建立一個.proto的文件
package csdnblog;
message PBUser {
required string userId = 1;
optional string nick = 2;
optional string avatar = 3;
}
創建完畢后,我們來編譯這個person.proto文件。cd到ProtoBuf的文件夾后,命令如下:
protoc --plugin=/usr/local/bin/protoc-gen-objc person.proto --objc_out=./
protoc會自動在/usr/local/bin/目錄下尋找名為”protoc-gen-objc”的插件,并使用該插件編譯.proto文件,最終生成兩個文件:
Person.pb.h
Person.pb.m
這個步驟通過后,說明ProtocoBuffer Compiler for Objective-C可以正常工作了。
現在我們可以在Xcode中使用ProtocolBuffer
打開Xcode!新建一個ProtoBuffer工程! 然后有兩個方法把protobuf添加到你的工程里面,一個是直接添加,一個是利用CocoaPods 強烈推薦后者,因為cocoapods能夠很方便管理第三方類庫,以后人家的工程升級了,你只需要一行 pod update 就ok了。順便打個廣告:CocoaPods的強大,不用不知道,一用嚇一跳
關于安裝和使用cocoapods,屬于另一個話題,看另一個博文。
我的cocoapods 版本是0.36 ?我的Podfile文件如下:
source 'https://gitcafe.com/akuandev/Specs.git'
# platform :ios, '7.0'
target "ProtoBuffer" do
pod "ProtocolBuffers", "~> 1.9.7"
end
在保存之后,到終端,cd到工程里面,
pod install
完成后,將前面編譯的 Person.pb.h和Person.pb.m導入工程中,到此你就可以使用了。
提示:~/protobuf-objc文件里有一個 iOS的栗子哦,有興趣的朋友可以研究下哦。
注:“source 'https://gitcafe.com/akuandev/Specs.git'” ?我這個cocoapods使用了一個叫akinliu在gitcafe上建立的CocoaPods索引庫的鏡像。因為gitcafe是國內的服務器,所以會快很多。
如下操作可以將CocoaPods設置成使用gitcafe鏡像:
pod repo remove master
pod repo add master https://gitcafe.com/akuandev/Specs.git
pod repo update
Either you, or somebody else, appears to have edited theautogen.shscript to directly run/Library/Developer/CommandLineTools/usr/bin/libtoolor made some other change to cause it to run that script; this was the Wrong Thing To Do, as that's the OS X libtool, and that ismostdefinitelyNOTthe libtool that Wireshark wants.
what do I need to do to fix it?
undo whatever was done to cause autogen.sh to make it run/Library/Developer/CommandLineTools/usr/bin/libtool;
rename whatever version of libtool you installed (probably/usr/local/bin/libtool) toglibtool, and rename thelibtoolizein the same directory toglibtoolize, so that it looks just like the GNU libtool that OS X used to provide, and thus so that Wireshark's attempt to use the GNU libtool works.
glibtool系統名稱沖突,需要強制命名
本文借鑒了以下兩篇博文,非常感謝他們的分享。希望大家可以參考一下:
http://www.2cto.com/kf/201503/382440.html
http://www.cnblogs.com/tara/archive/2012/03/20/2407951.html
chaowudeiMac:desktop chaowu$ git clone https://github.com/qzix/protobuf-objc.git
Cloning into 'protobuf-objc'...
remote: Counting objects: 951, done.
remote: Total 951 (delta 0), reused 0 (delta 0), pack-reused 951
Receiving objects: 100% (951/951), 848.88 KiB | 70.00 KiB/s, done.
Resolving deltas: 100% (551/551), done.
Checking connectivity... done.
chaowudeiMac:desktop chaowu$ cd protobuf-objc
chaowudeiMac:protobuf-objc chaowu$ ls
CREDITS?? ??? ?README.md?? ?configure.ac
Makefile.am?? ?autogen.sh?? ?src
chaowudeiMac:protobuf-objc chaowu$ ./autogen.sh
libtoolize: putting auxiliary files in '.'.
libtoolize: copying file './ltmain.sh'
libtoolize: putting macros in AC_CONFIG_MACRO_DIRS, 'm4'.
libtoolize: copying file 'm4/libtool.m4'
libtoolize: copying file 'm4/ltoptions.m4'
libtoolize: copying file 'm4/ltsugar.m4'
libtoolize: copying file 'm4/ltversion.m4'
libtoolize: copying file 'm4/lt~obsolete.m4'
configure.ac:13: installing './compile'
configure.ac:9: installing './config.guess'
configure.ac:9: installing './config.sub'
configure.ac:10: installing './install-sh'
configure.ac:10: installing './missing'
src/compiler/Makefile.am:6: warning: source file 'google/protobuf/objectivec-descriptor.pb.cc' is in a subdirectory,
src/compiler/Makefile.am:6: but option 'subdir-objects' is disabled
automake: warning: possible forward-incompatibility.
automake: At least a source file is in a subdirectory, but the 'subdir-objects'
automake: automake option hasn't been enabled.? For now, the corresponding output
automake: object file(s) will be placed in the top-level directory.? However,
automake: this behaviour will change in future Automake versions: they will
automake: unconditionally cause object files to be placed in the same subdirectory
automake: of the corresponding sources.
automake: You are advised to start using 'subdir-objects' option throughout your
automake: project, to avoid future incompatibilities.
src/compiler/Makefile.am: installing './depcomp'
chaowudeiMac:protobuf-objc chaowu$ ./depcomp
./depcomp: No command.? Try './depcomp --help' for more information.
chaowudeiMac:protobuf-objc chaowu$ ./configure
checking build system type... x86_64-apple-darwin14.1.0
checking host system type... x86_64-apple-darwin14.1.0
checking target system type... x86_64-apple-darwin14.1.0
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... ./install-sh -c -d
checking for gawk... no
checking for mawk... no
checking for nawk... no
checking for awk... awk
checking whether make sets $(MAKE)... yes
checking whether makesupports nested variables... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking whether gcc understands -c and -o together... yes
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking dependency style of g++... gcc3
checking C++ compilerflags...... use default: -g -O2 -DNDEBUG
checking how to print strings... printf
checking for a sed that does not truncate output... /usr/bin/sed
checking for grep that handles longlines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for fgrep... /usr/bin/grep -F
checking for ld used by gcc... /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld
checking if the linker (/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld) is GNU ld... no
checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm
checking the name lister (/usr/bin/nm) interface... BSD nm
checking whether ln -s works... yes
checking the maximum length of commandlinearguments... 196608
checking how to convert x86_64-apple-darwin14.1.0 file names to x86_64-apple-darwin14.1.0 format... func_convert_file_noop
checking how to convert x86_64-apple-darwin14.1.0 file names to toolchain format... func_convert_file_noop
checking for /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld option to reload object files... -r
checking for objdump... no
checking how to recognize dependent libraries... pass_all
checking for dlltool... no
checking how to associate runtime and link libraries... printf %s/n
checking for ar... ar
checking for archiver @FILEsupport... no
checking for strip... strip
checking for ranlib... ranlib
checking command to parse /usr/bin/nm output from gcc object... ok
checking for sysroot... no
checking for a working dd... /bin/dd
checking how to truncate binary pipes... /bin/dd bs=4096 count=1
checking for mt... no
checking if : is a manifest tool... no
checking for dsymutil... dsymutil
checking for nmedit... nmedit
checking for lipo... lipo
checking for otool... otool
checking for otool64... no
checking for -single_module linker flag... yes
checking for -exported_symbols_list linker flag... yes
checking for -force_load linkerflag... yes
checking how to run the C preprocessor... gcc -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for dlfcn.h... yes
checking for objdir... .libs
checking if gcc supports -fno-rtti -fno-exceptions... yes
checking for gcc option to produce PIC... -fno-common -DPIC
checking if gcc PIC flag -fno-common -DPIC works... yes
checking if gcc staticflag-static works... no
checking if gccsupports -c -o file.o... yes
checking if gcc supports -c -o file.o... (cached) yes
checking whether the gcc linker (/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld) supports shared libraries... yes
checking dynamic linker characteristics... darwin14.1.0 dyld
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtoolsupports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... yes
checking how to run the C++ preprocessor... g++ -E
checking for ld used by g++... /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld
checking if the linker (/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld) is GNU ld... no
checking whether the g++ linker (/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld)supports shared libraries... yes
checking for g++ option to produce PIC... -fno-common -DPIC
checking if g++ PIC flag -fno-common -DPIC works... yes
checking if g++ staticflag-static works... no
checking if g++ supports -c -o file.o... yes
checking if g++ supports -c -o file.o... (cached) yes
checking whether the g++ linker (/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld)supports shared libraries... yes
checking dynamic linker characteristics... darwin14.1.0 dyld
checking how to hardcode library paths into programs... immediate
checking for ANSI C header files... (cached) yes
checking fcntl.h usability... yes
checking fcntl.h presence... yes
checking for fcntl.h... yes
checking for inttypes.h... (cached) yes
checking limits.h usability... yes
checking limits.h presence... yes
checking for limits.h... yes
checking for stdlib.h... (cached) yes
checking for unistd.h... (cached) yes
checking for working memcmp... yes
checking for working strtod... yes
checking for ftruncate... yes
checking for memset... yes
checking for mkdir... yes
checking for strchr... yes
checking for strerror... yes
checking for strtol... yes
checking google/protobuf/stubs/common.h usability... yes
checking google/protobuf/stubs/common.h presence... yes
checking for google/protobuf/stubs/common.h... yes
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/compiler/Makefile
config.status: creating config.h
config.status: executing depfiles commands
config.status: executing libtool commands
chaowudeiMac:protobuf-objc chaowu$ make
/Applications/Xcode.app/Contents/Developer/usr/bin/make? all-recursive
Making all in src/compiler
g++ -DHAVE_CONFIG_H -I. -I../..???? -g -O2 -DNDEBUG -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.cc
mv -f .deps/main.Tpo .deps/main.Po
g++ -DHAVE_CONFIG_H -I. -I../..???? -g -O2 -DNDEBUG -MT objc_enum_field.o -MD -MP -MF .deps/objc_enum_field.Tpo -c -o objc_enum_field.o objc_enum_field.cc
mv -f .deps/objc_enum_field.Tpo .deps/objc_enum_field.Po
g++ -DHAVE_CONFIG_H -I. -I../..???? -g -O2 -DNDEBUG -MT objc_file.o -MD -MP -MF .deps/objc_file.Tpo -c -o objc_file.o objc_file.cc
mv -f .deps/objc_file.Tpo .deps/objc_file.Po
g++ -DHAVE_CONFIG_H -I. -I../..???? -g -O2 -DNDEBUG -MT objc_message_field.o -MD -MP -MF .deps/objc_message_field.Tpo -c -o objc_message_field.o objc_message_field.cc
mv -f .deps/objc_message_field.Tpo .deps/objc_message_field.Po
g++ -DHAVE_CONFIG_H -I. -I../..???? -g -O2 -DNDEBUG -MT objc_enum.o -MD -MP -MF .deps/objc_enum.Tpo -c -o objc_enum.o objc_enum.cc
mv -f .deps/objc_enum.Tpo .deps/objc_enum.Po
g++ -DHAVE_CONFIG_H -I. -I../..???? -g -O2 -DNDEBUG -MT objc_generator.o -MD -MP -MF .deps/objc_generator.Tpo -c -o objc_generator.o objc_generator.cc
mv -f .deps/objc_generator.Tpo .deps/objc_generator.Po
g++ -DHAVE_CONFIG_H -I. -I../..???? -g -O2 -DNDEBUG -MT objc_primitive_field.o -MD -MP -MF .deps/objc_primitive_field.Tpo -c -o objc_primitive_field.o objc_primitive_field.cc
mv -f .deps/objc_primitive_field.Tpo .deps/objc_primitive_field.Po
g++ -DHAVE_CONFIG_H -I. -I../..???? -g -O2 -DNDEBUG -MT objc_extension.o -MD -MP -MF .deps/objc_extension.Tpo -c -o objc_extension.o objc_extension.cc
mv -f .deps/objc_extension.Tpo .deps/objc_extension.Po
g++ -DHAVE_CONFIG_H -I. -I../..???? -g -O2 -DNDEBUG -MT objc_helpers.o -MD -MP -MF .deps/objc_helpers.Tpo -c -o objc_helpers.o objc_helpers.cc
objc_helpers.cc:363:13: warning: enumeration values 'OBJECTIVECTYPE_STRING',
'OBJECTIVECTYPE_DATA', and 'OBJECTIVECTYPE_MESSAGE' not handled in switch
[-Wswitch]
switch (type) {
^
objc_helpers.cc:423:13: warning: enumeration values 'OBJECTIVECTYPE_STRING',
'OBJECTIVECTYPE_DATA', and 'OBJECTIVECTYPE_MESSAGE' not handled in switch
[-Wswitch]
switch(GetObjectiveCType(field)) {
^
2 warnings generated.
mv -f .deps/objc_helpers.Tpo .deps/objc_helpers.Po
g++ -DHAVE_CONFIG_H -I. -I../..???? -g -O2 -DNDEBUG -MT objc_field.o -MD -MP -MF .deps/objc_field.Tpo -c -o objc_field.o objc_field.cc
mv -f .deps/objc_field.Tpo .deps/objc_field.Po
g++ -DHAVE_CONFIG_H -I. -I../..???? -g -O2 -DNDEBUG -MT objc_message.o -MD -MP -MF .deps/objc_message.Tpo -c -o objc_message.o objc_message.cc
mv -f .deps/objc_message.Tpo .deps/objc_message.Po
g++ -DHAVE_CONFIG_H -I. -I../..???? -g -O2 -DNDEBUG -MT objectivec-descriptor.pb.o -MD -MP -MF .deps/objectivec-descriptor.pb.Tpo -c -o objectivec-descriptor.pb.o `test -f 'google/protobuf/objectivec-descriptor.pb.cc' || echo './'`google/protobuf/objectivec-descriptor.pb.cc
mv -f .deps/objectivec-descriptor.pb.Tpo .deps/objectivec-descriptor.pb.Po
/bin/sh ../../libtool? --tag=CXX?? --mode=link g++? -g -O2 -DNDEBUG -lprotobuf -lprotoc? -o protoc-gen-objc main.o objc_enum_field.o objc_file.o objc_message_field.o objc_enum.o objc_generator.o objc_primitive_field.o objc_extension.o objc_helpers.o objc_field.o objc_message.o objectivec-descriptor.pb.o
libtool: link: g++ -g -O2 -DNDEBUG -o protoc-gen-objc main.o objc_enum_field.o objc_file.o objc_message_field.o objc_enum.o objc_generator.o objc_primitive_field.o objc_extension.o objc_helpers.o objc_field.o objc_message.o objectivec-descriptor.pb.o -Wl,-bind_at_load? -lprotobuf -lprotoc
make[2]: Nothing to be done for `all-am'.
chaowudeiMac:protobuf-objc chaowu$ make install
Making install in src/compiler
../.././install-sh -c -d '/usr/local/bin'
/bin/sh ../../libtool?? --mode=install /usr/bin/install -c protoc-gen-objc '/usr/local/bin'
libtool: install: /usr/bin/install -c protoc-gen-objc /usr/local/bin/protoc-gen-objc
make[2]: Nothing to be done for `install-data-am'.
make[2]: Nothing to be done for `install-exec-am'.
make[2]: Nothing to be done for `install-data-am'.
chaowudeiMac:protobuf-objc chaowu$ cd ..
chaowudeiMac:desktop chaowu$ protoc --proto_path=. --objc_out=. im.msg.proto
im.msg.proto: No such file or directory
chaowudeiMac:desktop chaowu$ protoc --proto_path=. --objc_out=. im_msg.proto
chaowudeiMac:desktop chaowu$? protoc --proto_path=. --objc_out=. im_msg.proto
chaowudeiMac:desktop chaowu$
首先,打開終端!
?
1
brew -v
:查看你的mac里面有沒有裝brew。brew是mac os里面,類似于ubuntu的apt-get的功能,都可以直接在終端輸入命令然后安裝程序。-v自然就是版本version的意思
?
1
ruby -e $(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)
這一句半懂不懂,,大概就是利用curl工具訪問那個url,然后在ruby環境下載安裝brew
?
123
brew install automakebrew install libtoolbrew install protobuf
Homebrew 的使用方法也很簡單。
基本用法
brew search formula # 搜索軟件包
brew install formula # 安裝軟件包
brew remove formula # 移除軟件包
brew cleanup formula # 清除舊包
brew list # 列出已安裝的軟件包
brew update # 更新 Homebrew
brew upgrade # 升級軟件包
brew home formula # 用瀏覽器打開
brew info formula # 顯示軟件內容信息
brew deps formula # 顯示包的依賴
brew server # 啟動 web 服務器,可以通過瀏覽器訪問 http://localhost:4567 來通過網頁來管理包
brew -h # 幫助
brew versions formula # 列出軟件包的版本
4.新建一個工程,將生成的personOC版的文件導入,然后將ProtocolBuffers-2.2.0-Source/objectivec下的文件放到項目的目錄下,創建一個ProtobufLib文件夾,放進去,最好放在一個文件夾下面像這樣
1 message Person { 2 required string name = 1; 3 required int32 id = 2; 4 optional string email = 3; 56 enum PhoneType { 7 MOBILE = 0; 8 HOME = 1; 9 WORK = 2;10 }11 12 message PhoneNumber {13 required string number = 1;14 optional PhoneType type = 2 [default = HOME];15 }16 17 repeated PhoneNumber phone = 4;18 }
B.在ProtocolBuffers-2.2.0-Source下創建這樣一個子目錄build/objc以便存放我們生成的classes
現在執行命令:
src/protoc --proto_path=src --objc_out=build/objc src/Person.proto
成功后會在build/objc下生成Person.pd.h 和 Person.pb.m 兩個Object-C文件
3、測試
A.新建一個項目ProtobufDemo,將剛才生成的兩個文件加入項目。然后將ProtocolBuffers-2.2.0-Source/objectivec 下的文件放到項目的目錄下,最好放在一個文件夾下面像這樣
創建一個ProtobufLib文件夾,放進去.
B.之后把ProtocolBuffers.xcodeproj添加到項目中,我習慣將它放到Frameworks下。
C.然后雙擊Targets下的ProtobufDemo,點擊+添加,之后做一些配置,like this
在.pch文件中導入?#import "ProtocolBuffers.h"
配置好這些之后編譯你的項目,應該不會報錯了吧。