QT On IOS + protobuf3的使用

来源:互联网 发布:弹幕位置显示算法 编辑:程序博客网 时间:2024/06/02 05:27

如果你看到了这个,并且你还有在android上面使用的需求,请看我另外一篇博文。

因为需要用到,确实也百度和谷歌找了很多,基本没什么关于protobuf3+QT的使用的,大部分都是讲protobuf2.6的。功夫不负有心人,终于搞定了protobuf3+qt的使用。

1、编写编译脚本

该脚本源自于git,点击前往git

如果直接使用该脚本,则可以生成protobuf2.6的,可以使用。

如果要想生成protobuf3的,则需要修改一些地方。

需要修改的地方为

#!/bin/bashecho "$(tput setaf 2)"echo "###################################################################"echo "# Preparing to build Google Protobuf"echo "###################################################################"echo "$(tput sgr0)"export SED=`which sed`# 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} ]then    rm -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.#如果需要使用git上面最新的,将下面设置为YES,否则使用指定的版本,版本后在下面指定USE_GIT_MASTER=YESPROTOBUF_GIT_URL=https://github.com/google/protobuf.gitPROTOBUF_GIT_DIRNAME=protobuf#版本号可以自行更改,原来的是默认2.6.0,我改成了3.3.0,不更改也可以PROTOBUF_VERSION=3.3.0PROTOBUF_RELEASE_URL=https://codeload.github.com/google/protobuf/zip/v${PROTOBUF_VERSION}PROTOBUF_RELEASE_DIRNAME=protobuf-${PROTOBUF_VERSION}# if BUILD_MACOSX_X86_64=NO,you must install protoc in your system,because we need protoc#需要编译的需要写成YES,不需要的写成NO,最后会生成通用库,建议全部选择YESBUILD_MACOSX_X86_64=YESBUILD_I386_IOSSIM=YESBUILD_X86_64_IOSSIM=YESBUILD_IOS_ARMV7=YESBUILD_IOS_ARMV7S=YESBUILD_IOS_ARM64=YESPROTOBUF_SRC_DIR=/tmp/protobuf#原本默认是13.4.0  我根据我自己的版本改成了16.4.0DARWIN=darwin16.4.0XCODEDIR=`xcode-select --print-path`IOS_SDK_VERSION=`xcrun --sdk iphoneos --show-sdk-version`#默认的是7.1,由于QT好像默认的是7.0,所有需要修改成7.0MIN_SDK_VERSION=7.0MACOSX_PLATFORM=`xcrun --sdk macosx --show-sdk-platform-path`MACOSX_SYSROOT=`xcrun --sdk macosx --show-sdk-path`IPHONEOS_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; do    read -p "Proceed with build? (y/n) " yn    case $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} ]    then        rm -rf ${PROTOBUF_SRC_DIR}    fi    cd `dirname ${PROTOBUF_SRC_DIR}`    if [ "${USE_GIT_MASTER}" == "YES" ]    then        git clone ${PROTOBUF_GIT_URL}    else        if [ -d ${PROTOBUF_RELEASE_DIRNAME} ]        then            rm -rf "${PROTOBUF_RELEASE_DIRNAME}"        fi        if [ ! -f "${PROTOBUF_RELEASE_DIRNAME}.zip" ]; then             curl --location ${PROTOBUF_RELEASE_URL} --output ${PROTOBUF_RELEASE_DIRNAME}.zip        fi        unzip ${PROTOBUF_RELEASE_DIRNAME}.zip        mv "${PROTOBUF_RELEASE_DIRNAME}" "${PROTOBUF_SRC_DIR}"        # 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" ]        then            rm -r "${PROTOBUF_SRC_DIR}/gtest"        fi    fi)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}    # Check that we are being run from the right directory.    if test ! -f src/google/protobuf/stubs/common.h    then        cat >&2 << __EOF__Could not find source code.  Make sure you are running this script from theroot of the distribution tree.__EOF__        exit 1    fi    # 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 gtest    then        echo "Google Test not present.  Fetching gtest-1.7.0 from the web..."        if [ ! -f "googletest-release-1.7.0.zip" ]; then             curl --location https://codeload.github.com/google/googletest/zip/release-1.7.0 --output googletest-release-1.7.0.zip        fi        unzip googletest-release-1.7.0.zip        mv googletest-release-1.7.0 gtest    fi    autoreconf -f -i -Wall,no-obsolete    rm -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,在编译2.6版本时没有问题,但是在编译3.x版本时有问题,改成make clean后就OK了,以下是为了判断增加了一句#其实,改成make clean就可以,后续同理        if [ "${USE_GIT_MASTER}" == "YES" ];thenmake cleanelseif [ "${PROTOBUF_VERSION}" > "3.0.0" ];thenmake cleanelsemake distcleanfifi        ./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}"        make        make check        make install    )    PROTOC=${PREFIX}/platform/x86_64-mac/bin/protocelse    PROTOC=protocfiif [ ! -n `which $PROTOC` ]; then echo 'error:command protoc not available' exit 1fi#################################################################### 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}        if [ "${USE_GIT_MASTER}" == "YES" ];thenmake cleanelseif [ "${PROTOBUF_VERSION}" > "3.0.0" ];thenmake cleanelsemake distcleanfifi        ./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} -mios-simulator-version-min=${MIN_SDK_VERSION} -arch i386 -isysroot ${IPHONESIMULATOR_SYSROOT}" "CXX=${CXX}" "CXXFLAGS=${CXXFLAGS} -mios-simulator-version-min=${MIN_SDK_VERSION} -arch i386 -isysroot ${IPHONESIMULATOR_SYSROOT}" LDFLAGS="-arch i386 -mios-simulator-version-min=${MIN_SDK_VERSION} ${LDFLAGS} -L${IPHONESIMULATOR_SYSROOT}/usr/lib/ -L${IPHONESIMULATOR_SYSROOT}/usr/lib/system" "LIBS=${LIBS}"        make        make 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}        if [ "${USE_GIT_MASTER}" == "YES" ];thenmake cleanelseif [ "${PROTOBUF_VERSION}" > "3.0.0" ];thenmake cleanelsemake distcleanfifi        ./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} -mios-simulator-version-min=${MIN_SDK_VERSION} -arch x86_64 -isysroot ${IPHONESIMULATOR_SYSROOT}" "CXX=${CXX}" "CXXFLAGS=${CXXFLAGS} -mios-simulator-version-min=${MIN_SDK_VERSION} -arch x86_64 -isysroot ${IPHONESIMULATOR_SYSROOT}" LDFLAGS="-arch x86_64 -mios-simulator-version-min=${MIN_SDK_VERSION} ${LDFLAGS} -L${IPHONESIMULATOR_SYSROOT}/usr/lib/ -L${IPHONESIMULATOR_SYSROOT}/usr/lib/system" "LIBS=${LIBS}"        make        make install    )fiecho "$(tput setaf 2)"echo "##################"echo "# armv7 for iPhone"echo "##################"echo "$(tput sgr0)"if [ "${BUILD_IOS_ARMV7}" == "YES" ]then    (        cd ${PROTOBUF_SRC_DIR}        if [ "${USE_GIT_MASTER}" == "YES" ];thenmake cleanelseif [ "${PROTOBUF_VERSION}" > "3.0.0" ];thenmake cleanelsemake distcleanfifi        ./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}"        make        make install    )fiecho "$(tput setaf 2)"echo "###################"echo "# armv7s for iPhone"echo "###################"echo "$(tput sgr0)"if [ "${BUILD_IOS_ARMV7S}" == "YES" ]then    (        cd ${PROTOBUF_SRC_DIR}        if [ "${USE_GIT_MASTER}" == "YES" ];thenmake cleanelseif [ "${PROTOBUF_VERSION}" > "3.0.0" ];thenmake cleanelsemake distcleanfifi        ./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} -miphoneos-version-min=${MIN_SDK_VERSION} -arch armv7s -isysroot ${IPHONEOS_SYSROOT}" LDFLAGS="-arch armv7s -miphoneos-version-min=${MIN_SDK_VERSION} ${LDFLAGS}" "LIBS=${LIBS}"        make        make install    )fiecho "$(tput setaf 2)"echo "##################"echo "# arm64 for iPhone"echo "##################"echo "$(tput sgr0)"if [ "${BUILD_IOS_ARM64}" == "YES" ]then    (        cd ${PROTOBUF_SRC_DIR}if [ "${USE_GIT_MASTER}" == "YES" ];thenmake cleanelseif [ "${PROTOBUF_VERSION}" > "3.0.0" ];thenmake cleanelsemake distcleanfifi        ./configure --build=x86_64-apple-${DARWIN} --host=arm-apple-${DARWIN} --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} -miphoneos-version-min=${MIN_SDK_VERSION} -arch arm64 -isysroot ${IPHONEOS_SYSROOT}" LDFLAGS="-arch arm64 -miphoneos-version-min=${MIN_SDK_VERSION} ${LDFLAGS}" "LIBS=${LIBS}"        make        make install    )fiecho "$(tput setaf 2)"echo "###################################################################"echo "# Create Universal Libraries and Finalize the packaging"echo "###################################################################"echo "$(tput sgr0)"(    cd ${PREFIX}/platform    mkdir universal    ALL_LIB=""    for LIB in `find . -name libprotobuf.a`; do        ALL_LIB="${ALL_LIB} ${LIB}"    done    if [ ! -z "${ALL_LIB}" ]; then         lipo ${ALL_LIB} -create -output universal/libprotobuf.a    fi    ALL_LIB=""    for LIB in `find . -name libprotobuf-lite.a`; do        ALL_LIB="${ALL_LIB} ${LIB}"    done    if [ ! -z "${ALL_LIB}" ]; then         lipo ${ALL_LIB} -create -output universal/libprotobuf-lite.a    fi    echo ${ALL_LIB})(    cd ${PREFIX}    mkdir bin    mkdir lib    if [ "${BUILD_MACOSX_X86_64}" == "YES" ]        then        cp -r platform/x86_64-mac/bin/protoc bin        cp -r platform/x86_64-mac/lib/* lib    fi    cp -r platform/universal/* lib    lipo -info lib/libprotobuf.a    lipo -info lib/libprotobuf-lite.a    open lib/)if [ "${USE_GIT_MASTER}" == "YES" ]then    if [ -d "${PREFIX}-master" ]    then        rm -rf "${PREFIX}-master"    fi    mv "${PREFIX}" "${PREFIX}-master"else    if [ -d "${PREFIX}-${PROTOBUF_VERSION}" ]    then        rm -rf "${PREFIX}-${PROTOBUF_VERSION}"    fi    mv "${PREFIX}" "${PREFIX}-${PROTOBUF_VERSION}"fiecho Done!

使用方法就是,新建一个文件夹,把上面的代码赋值到 xxx.sh文件中去,然后添加权限,然后执行即可。
注意安装必要的依赖库,比如执行语句 

brew install autoconf automake libtool curl make g++ unzip

2、在QT中的使用办法:
与调用普通库一样的,例如

ios:{CONFIG -= bitcode       INCLUDEPATH += /Users/macos/mylibs/protobuf/include       LIBS += -L/Users/macos/mylibs/protobuf/platform/universal \-lprotobuf}

注意,其中有一条很重要,
CONFIG -= bitcode
这一句是必须的,这都是百度谷歌很多次,才在一个角落里面发现的。
附录:
我修改过的脚本下载地址:链接:http://pan.baidu.com/s/1mioQrR6 密码:vfpr
原本的脚本地址为git:https://github.com/zhangpengyf/protobuf-build-script
编译好的库文件地址:(包含单个平台与通用的库,以及头文件)链接:http://pan.baidu.com/s/1miiKITy 密码:wrbg
如果连接失效,请联系593079199@qq.com


原创粉丝点击