Mac OS下安装ProtocolBuffer与iOS的简单使用 - Go语言中文社区

Mac OS下安装ProtocolBuffer与iOS的简单使用


转自: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"

配置好这些之后编译你的项目,应该不会报错了吧。

版权声明:本文来源简书,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://www.jianshu.com/p/d5642a7d1e10
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-01-08 08:33:52
  • 阅读 ( 1351 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢