iOS开发中使用protobuf遇到的一些问题
来源:互联网 发布:java事件委托原理 编辑:程序博客网 时间:2024/04/27 17:03
Protocol Buffers 是googol提供的,一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化。它很适合做数据存储或 RPC 数据交换格式。可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。目前提供了 C++、Java、Python 三种语言的 API。
项目中对通讯协议部分的数据处理时,使用protobuf对socket的字节流进行序列化和反序列化。由于项目中的protobuf是静态库的形式使用,所以第一反应就是下载官方的源文件,安装readme指示进行操作,然而只能生产proto文件编译器(个人叫法),却编译不出来我需要的静态库。
按照网上提供方式的处理,自己建立库工程,提取protobuf源文件,删除test相关的文件,然后进行编译。此方式太容易出错了,多次尝试后,终于不了了之。在进行了两天的苦苦寻觅后,终于让我找到一个国外的大叔做的一个(一键式)编译脚本。https://gist.github.com/BennettSmith/9487468ae3375d0db0cc/download#
不如意事十八九,能与人言无二三。在我柳暗花明之后,以为终于要一路平坦到达胜利的时候,再一次让我心如刀绞,肝肠寸断。编译不过啊,下载不了啊,各种问题啊。
首先是提示系统缺少一些编译必须的工具,Autoconf, Automake & Libtool。不用什么说的,安装吧。
然后又是下载不了的问题。这里我结合脚本说一下:(大部分地方注释的很清楚,没有更多需要说的)
#!/bin/bashecho "$(tput setaf 2)"echo "###################################################################"echo "# Preparing to build Google Protobuf"echo "###################################################################"echo "$(tput sgr0)"# The results will be stored relative to the location# where you stored this script, **not** relative to# the location of the protobuf git repo.PREFIX=`pwd`/protobufif [ -d ${PREFIX} ]thenrm -rf "${PREFIX}"fimkdir -p "${PREFIX}/platform"# A "YES" value will build the latest code from GitHub on the master branch.# A "NO" value will use the 2.6.0 tarball downloaded from googlecode.com.#这个变量确定是从github获取源码,还是从googol官网获取USE_GIT_MASTER=NOPROTOBUF_GIT_URL=https://github.com/google/protobuf.gitPROTOBUF_GIT_DIRNAME=protobufPROTOBUF_VERSION=2.6.0PROTOBUF_RELEASE_URL=https://protobuf.googlecode.com/svn/rc/protobuf-${PROTOBUF_VERSION}.tar.gzPROTOBUF_RELEASE_DIRNAME=protobuf-${PROTOBUF_VERSION}#以下这几个变量决定了,打包库中包含哪些架构的库BUILD_MACOSX_X86_64=YESBUILD_I386_IOSSIM=YESBUILD_X86_64_IOSSIM=YESBUILD_IOS_ARMV7=YESBUILD_IOS_ARMV7S=YESBUILD_IOS_ARM64=YESPROTOBUF_SRC_DIR=/tmp/protobufDARWIN=darwin13.4.0XCODEDIR=`xcode-select --print-path`IOS_SDK_VERSION=`xcrun --sdk iphoneos --show-sdk-version`MIN_SDK_VERSION=7.1MACOSX_PLATFORM=${XCODEDIR}/Platforms/MacOSX.platformMACOSX_SYSROOT=${MACOSX_PLATFORM}/Developer/MacOSX10.9.sdkIPHONEOS_PLATFORM=`xcrun --sdk iphoneos --show-sdk-platform-path`IPHONEOS_SYSROOT=`xcrun --sdk iphoneos --show-sdk-path`IPHONESIMULATOR_PLATFORM=`xcrun --sdk iphonesimulator --show-sdk-platform-path`IPHONESIMULATOR_SYSROOT=`xcrun --sdk iphonesimulator --show-sdk-path`# Uncomment if you want to see more information about each invocation# of clang as the builds proceed.# CLANG_VERBOSE="--verbose"CC=clangCXX=clangCFLAGS="${CLANG_VERBOSE} -DNDEBUG -g -O0 -pipe -fPIC -fcxx-exceptions"CXXFLAGS="${CLANG_VERBOSE} ${CFLAGS} -std=c++11 -stdlib=libc++"LDFLAGS="-stdlib=libc++"LIBS="-lc++ -lc++abi"echo "PREFIX ..................... ${PREFIX}"echo "USE_GIT_MASTER ............. ${USE_GIT_MASTER}"echo "PROTOBUF_GIT_URL ........... ${PROTOBUF_GIT_URL}"echo "PROTOBUF_GIT_DIRNAME ....... ${PROTOBUF_GIT_DIRNAME}"echo "PROTOBUF_VERSION ........... ${PROTOBUF_VERSION}"echo "PROTOBUF_RELEASE_URL ....... ${PROTOBUF_RELEASE_URL}"echo "PROTOBUF_RELEASE_DIRNAME ... ${PROTOBUF_RELEASE_DIRNAME}"echo "BUILD_MACOSX_X86_64 ........ ${BUILD_MACOSX_X86_64}"echo "BUILD_I386_IOSSIM .......... ${BUILD_I386_IOSSIM}"echo "BUILD_X86_64_IOSSIM ........ ${BUILD_X86_64_IOSSIM}"echo "BUILD_IOS_ARMV7 ............ ${BUILD_IOS_ARMV7}"echo "BUILD_IOS_ARMV7S ........... ${BUILD_IOS_ARMV7S}"echo "BUILD_IOS_ARM64 ............ ${BUILD_IOS_ARM64}"echo "PROTOBUF_SRC_DIR ........... ${PROTOBUF_SRC_DIR}"echo "DARWIN ..................... ${DARWIN}"echo "XCODEDIR ................... ${XCODEDIR}"echo "IOS_SDK_VERSION ............ ${IOS_SDK_VERSION}"echo "MIN_SDK_VERSION ............ ${MIN_SDK_VERSION}"echo "MACOSX_PLATFORM ............ ${MACOSX_PLATFORM}"echo "MACOSX_SYSROOT ............. ${MACOSX_SYSROOT}"echo "IPHONEOS_PLATFORM .......... ${IPHONEOS_PLATFORM}"echo "IPHONEOS_SYSROOT ........... ${IPHONEOS_SYSROOT}"echo "IPHONESIMULATOR_PLATFORM ... ${IPHONESIMULATOR_PLATFORM}"echo "IPHONESIMULATOR_SYSROOT .... ${IPHONESIMULATOR_SYSROOT}"echo "CC ......................... ${CC}"echo "CFLAGS ..................... ${CFLAGS}"echo "CXX ........................ ${CXX}"echo "CXXFLAGS ................... ${CXXFLAGS}"echo "LDFLAGS .................... ${LDFLAGS}"echo "LIBS ....................... ${LIBS}"while true; doread -p "Proceed with build? (y/n) " yncase $yn in[Yy]* ) break;;[Nn]* ) exit;;* ) echo "Please answer yes or no.";;esacdoneecho "$(tput setaf 2)"echo "###################################################################"echo "# Fetch Google Protobuf"echo "###################################################################"echo "$(tput sgr0)"(if [ -d ${PROTOBUF_SRC_DIR} ]thenrm -rf ${PROTOBUF_SRC_DIR}ficd `dirname ${PROTOBUF_SRC_DIR}`#如果上面选择从github获取,则这里从github获取,否则从官网下载。#这两个地方获取的源码是不同的。官网的自带gtest。github则不带。#我使用的时候,从官网下载是一直没有下载成功过#从github下载一直抱一个系统缺少宏的错。if [ "${USE_GIT_MASTER}" == "YES" ]thengit clone ${PROTOBUF_GIT_URL}elseif [ -d ${PROTOBUF_RELEASE_DIRNAME} ]thenrm -rf "${PROTOBUF_RELEASE_DIRNAME}"ficurl --location ${PROTOBUF_RELEASE_URL} --output ${PROTOBUF_RELEASE_DIRNAME}.tar.gztar xvf ${PROTOBUF_RELEASE_DIRNAME}.tar.gzmv "${PROTOBUF_RELEASE_DIRNAME}" "${PROTOBUF_SRC_DIR}"rm ${PROTOBUF_RELEASE_DIRNAME}.tar.gz# Remove the version of Google Test included with the release.# We will replace it with version 1.7.0 in a later step.if [ -d "${PROTOBUF_SRC_DIR}/gtest" ]thenrm -r "${PROTOBUF_SRC_DIR}/gtest"fifi)echo "$(tput setaf 2)"echo "###################################################################"echo "# Fetch Google Test & Prepare the Configure Script"echo "# (note: This section is lifted from autogen.sh)"echo "###################################################################"echo "$(tput sgr0)"(cd ${PROTOBUF_SRC_DIR}#这里是去获取gtest的最新版本,从官网获取时自带gtest,但是上面也给去掉了。这里重新获取。#这一步我也是一直没有下载成功# Check that we're being run from the right directory.if test ! -f src/google/protobuf/stubs/common.hthencat >&2 << __EOF__Could not find source code. Make sure you are running this script from theroot of the distribution tree.__EOF__exit 1fi# Check that gtest is present. Older versions of protobuf were stored in SVN# and the gtest directory was setup as an SVN external. Now, protobuf is# stored in GitHub and the gtest directory is not included. The commands# below will grab the latest version of gtest. Currently that is 1.7.0.if test ! -e gtestthenecho "Google Test not present. Fetching gtest-1.7.0 from the web..."curl --location http://googletest.googlecode.com/files/gtest-1.7.0.zip --output gtest-1.7.0.zipunzip gtest-1.7.0.ziprm gtest-1.7.0.zipmv gtest-1.7.0 gtestfiautoreconf -f -i -Wall,no-obsoleterm -rf autom4te.cache config.h.in~)#################################################################### This section contains the build commands to create the native# protobuf library for Mac OS X. This is done first so we have# a copy of the protoc compiler. It will be used in all of the# susequent iOS builds.####################################################################以下就是开始编译各种版本的库并打包。顺序执行下去echo "$(tput setaf 2)"echo "###################################################################"echo "# x86_64 for Mac OS X"echo "###################################################################"echo "$(tput sgr0)"if [ "${BUILD_MACOSX_X86_64}" == "YES" ]then(cd ${PROTOBUF_SRC_DIR}make distclean./configure --disable-shared --prefix=${PREFIX} --exec-prefix=${PREFIX}/platform/x86_64-mac "CC=${CC}" "CFLAGS=${CFLAGS} -arch x86_64" "CXX=${CXX}" "CXXFLAGS=${CXXFLAGS} -arch x86_64" "LDFLAGS=${LDFLAGS}" "LIBS=${LIBS}"makemake checkmake install)fiPROTOC=${PREFIX}/platform/x86_64-mac/bin/protoc#################################################################### This section contains the build commands for each of the# architectures that will be included in the universal binaries.###################################################################echo "$(tput setaf 2)"echo "###########################"echo "# i386 for iPhone Simulator"echo "###########################"echo "$(tput sgr0)"if [ "${BUILD_I386_IOSSIM}" == "YES" ]then(cd ${PROTOBUF_SRC_DIR}make distclean./configure --build=x86_64-apple-${DARWIN} --host=i386-apple-${DARWIN} --with-protoc=${PROTOC} --disable-shared --prefix=${PREFIX} --exec-prefix=${PREFIX}/platform/i386-sim "CC=${CC}" "CFLAGS=${CFLAGS} -miphoneos-version-min=${MIN_SDK_VERSION} -arch i386 -isysroot ${IPHONESIMULATOR_SYSROOT}" "CXX=${CXX}" "CXXFLAGS=${CXXFLAGS} -arch i386 -isysroot ${IPHONESIMULATOR_SYSROOT}" LDFLAGS="-arch i386 -miphoneos-version-min=${MIN_SDK_VERSION} ${LDFLAGS}" "LIBS=${LIBS}"makemake install)fiecho "$(tput setaf 2)"echo "#############################"echo "# x86_64 for iPhone Simulator"echo "#############################"echo "$(tput sgr0)"if [ "${BUILD_X86_64_IOSSIM}" == "YES" ]then(cd ${PROTOBUF_SRC_DIR}make distclean./configure --build=x86_64-apple-${DARWIN} --host=x86_64-apple-${DARWIN} --with-protoc=${PROTOC} --disable-shared --prefix=${PREFIX} --exec-prefix=${PREFIX}/platform/x86_64-sim "CC=${CC}" "CFLAGS=${CFLAGS} -miphoneos-version-min=${MIN_SDK_VERSION} -arch x86_64 -isysroot ${IPHONESIMULATOR_SYSROOT}" "CXX=${CXX}" "CXXFLAGS=${CXXFLAGS} -arch x86_64 -isysroot ${IPHONESIMULATOR_SYSROOT}" LDFLAGS="-arch x86_64 -miphoneos-version-min=${MIN_SDK_VERSION} ${LDFLAGS}" "LIBS=${LIBS}"makemake install)fiecho "$(tput setaf 2)"echo "##################"echo "# armv7 for iPhone"echo "##################"echo "$(tput sgr0)"if [ "${BUILD_IOS_ARMV7}" == "YES" ]then(cd ${PROTOBUF_SRC_DIR}make distclean./configure --build=x86_64-apple-${DARWIN} --host=armv7-apple-${DARWIN} --with-protoc=${PROTOC} --disable-shared --prefix=${PREFIX} --exec-prefix=${PREFIX}/platform/armv7-ios "CC=${CC}" "CFLAGS=${CFLAGS} -miphoneos-version-min=${MIN_SDK_VERSION} -arch armv7 -isysroot ${IPHONEOS_SYSROOT}" "CXX=${CXX}" "CXXFLAGS=${CXXFLAGS} -arch armv7 -isysroot ${IPHONEOS_SYSROOT}" LDFLAGS="-arch armv7 -miphoneos-version-min=${MIN_SDK_VERSION} ${LDFLAGS}" "LIBS=${LIBS}"makemake install)fiecho "$(tput setaf 2)"echo "###################"echo "# armv7s for iPhone"echo "###################"echo "$(tput sgr0)"if [ "${BUILD_IOS_ARMV7S}" == "YES" ]then(cd ${PROTOBUF_SRC_DIR}make distclean./configure --build=x86_64-apple-${DARWIN} --host=armv7s-apple-${DARWIN} --with-protoc=${PROTOC} --disable-shared --prefix=${PREFIX} --exec-prefix=${PREFIX}/platform/armv7s-ios "CC=${CC}" "CFLAGS=${CFLAGS} -miphoneos-version-min=${MIN_SDK_VERSION} -arch armv7s -isysroot ${IPHONEOS_SYSROOT}" "CXX=${CXX}" "CXXFLAGS=${CXXFLAGS} -arch armv7s -isysroot ${IPHONEOS_SYSROOT}" LDFLAGS="-arch armv7s -miphoneos-version-min=${MIN_SDK_VERSION} ${LDFLAGS}" "LIBS=${LIBS}"makemake install)fiecho "$(tput setaf 2)"echo "##################"echo "# arm64 for iPhone"echo "##################"echo "$(tput sgr0)"if [ "${BUILD_IOS_ARM64}" == "YES" ]then(cd ${PROTOBUF_SRC_DIR}make distclean./configure --build=x86_64-apple-${DARWIN} --host=arm --with-protoc=${PROTOC} --disable-shared --prefix=${PREFIX} --exec-prefix=${PREFIX}/platform/arm64-ios "CC=${CC}" "CFLAGS=${CFLAGS} -miphoneos-version-min=${MIN_SDK_VERSION} -arch arm64 -isysroot ${IPHONEOS_SYSROOT}" "CXX=${CXX}" "CXXFLAGS=${CXXFLAGS} -arch arm64 -isysroot ${IPHONEOS_SYSROOT}" LDFLAGS="-arch arm64 -miphoneos-version-min=${MIN_SDK_VERSION} ${LDFLAGS}" "LIBS=${LIBS}"makemake install)fiecho "$(tput setaf 2)"echo "###################################################################"echo "# Create Universal Libraries and Finalize the packaging"echo "###################################################################"echo "$(tput sgr0)"(cd ${PREFIX}/platformmkdir universallipo x86_64-sim/lib/libprotobuf.a i386-sim/lib/libprotobuf.a arm64-ios/lib/libprotobuf.a armv7s-ios/lib/libprotobuf.a armv7-ios/lib/libprotobuf.a -create -output universal/libprotobuf.alipo x86_64-sim/lib/libprotobuf-lite.a i386-sim/lib/libprotobuf-lite.a arm64-ios/lib/libprotobuf-lite.a armv7s-ios/lib/libprotobuf-lite.a armv7-ios/lib/libprotobuf-lite.a -create -output universal/libprotobuf-lite.a)(cd ${PREFIX}mkdir binmkdir libcp -r platform/x86_64-mac/bin/protoc bincp -r platform/x86_64-mac/lib/* libcp -r platform/universal/* librm -rf platformlipo -info lib/libprotobuf.alipo -info lib/libprotobuf-lite.a)if [ "${USE_GIT_MASTER}" == "YES" ]thenif [ -d "${PREFIX}-master" ]thenrm -rf "${PREFIX}-master"fimv "${PREFIX}" "${PREFIX}-master"elseif [ -d "${PREFIX}-${PROTOBUF_VERSION}" ]thenrm -rf "${PREFIX}-${PROTOBUF_VERSION}"fimv "${PREFIX}" "${PREFIX}-${PROTOBUF_VERSION}"fiecho Done!编译脚本写的很清晰。但是既然我使用脚本下载不能成功,我就只能先把源码下载下来,然后修改脚本,从本地加载文件。一切妥当后,执行编译,顺利完成,生成了各种需要的库。至此protobuf的集成使用告一段落。
0 0
- iOS开发中使用protobuf遇到的一些问题
- ios 开发中遇到的一些问题
- 我在ios游戏开发中遇到的一些问题
- ios开发学习过程中遇到的一些问题
- iOS开发中遇到的一些问题以及解决办法总结
- iOS开发中经常遇到的一些问题
- iOS开发中经常遇到的一些问题
- iOS开发中遇到的一些小问题
- ios开发遇到的一些问题0228
- [开发总结]WebView使用中遇到的一些问题&解决
- [开发总结]WebView使用中遇到的一些问题&解决
- ios中最近遇到的一些问题
- 软件开发中遇到的一些问题
- 开发过程中遇到的一些问题
- 开发ReportViewer中遇到的一些问题
- android开发中遇到的一些问题
- 开发ReportViewer中遇到的一些问题
- 游戏开发中遇到的一些问题
- 黑马程序员 —— Java高级视频_IO输入与输出(第二十一天)
- MVC学习系列-表单的异步提交
- 如何使用online account来创建微博Scope
- 挂载cd,复制cd内容
- 解决R文件无法生成的问题
- iOS开发中使用protobuf遇到的一些问题
- 关于大华onvif搜索不到的解决
- Indy TIdPOP3和TIdMessage如何接收邮件
- ToolBar详解
- GCC编译的背后( 预处理和编译 汇编和链接 )
- POJ2186 Popular Cows【Tarjan】【强连通分量】
- iBatis搭建JAVA项目
- Oledb读写excel数据
- 日常英语口语学习1:在家中——从起床到出门