原來的網址:http://www.th7.cn/Program/IOS/201506/484001.shtmlphp
首先是怎麼安裝Protobuf。 來自https://github.com/alexeyxo/protobuf-objc的文檔。html
打開終端!ios
brew -vgit
查看你的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 protobufgithub
就是利用brew下載安裝了。protobuf就是咱們想要的,另外兩個是依賴庫web
git clone https://github.com/alexeyxo/protobuf-objc.gitubuntu
./build.shxcode
從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的文件夾後,命令以下:ruby
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 the autogen.sh
script to directly run/Library/Developer/CommandLineTools/usr/bin/libtool
or 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 is most definitelyNOT the libtool that Wireshark wants.
what do I need to do to fix it?
/Library/Developer/CommandLineTools/usr/bin/libtool
;/usr/local/bin/libtool
) toglibtool
, and rename thelibtoolize
in 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 make supports 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++ compiler flags...... 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 long lines 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 command line arguments... 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 @FILE support... 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 linker flag... 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 static flag -static works... no
checking if gcc supports -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 libtool supports 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++ static flag -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 automake brew install libtool brew install protobuf |
Homebrew 的使用方法也很簡單。
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; 5 6 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文件
三、測試
A.新建一個項目ProtobufDemo,將剛纔生成的兩個文件加入項目。而後將ProtocolBuffers-2.2.0-Source/objectivec 下的文件放到項目的目錄下,最好放在一個文件夾下面像這樣
建立一個ProtobufLib文件夾,放進去.
B.以後把ProtocolBuffers.xcodeproj添加到項目中,我習慣將它放到Frameworks下。
C.而後雙擊Targets下的ProtobufDemo,點擊+添加,以後作一些配置,like this
在.pch文件中導入 #import "ProtocolBuffers.h"
配置好這些以後編譯你的項目,應該不會報錯了吧。