linux下protobuf 下载并cmake编译安装全过程脚本

protobuf源码下载

github上下载最新版本的protobuf源码zip包,并解压缩到本地
fetch_protobuf.sh

#!/bin/bash

shell_folder=$(cd "$(dirname "$0")";pwd)
pushd $shell_folder
#如果指定的文件/文件夹存在则删除,删除失败则中止脚本
remove_if_exist(){
    if [ $# -eq 1 ]
    then
        if [ -e $1 ]
        then
            rm $1 -fr
            if [ ! $? -eq 0 ]
            then
                exit -1
            fi
        fi
        return 0
    else 
        echo invalid argument:
        echo $*
        exit -1
    fi
}
exit_on_error(){
  if [ ! $? -eq 0 ]
  then
    exit -1
  fi
}
# 如果文件存在且checksum与$2指定的md5相等则返回 1,否则返回0
# $1 待检查的文件路径
# $2 md5校验码
need_download(){
  if [ $# -eq 2 ]
    then
      if [ -f $1 ]; then
        echo "File already exists. Checking md5..."
        os=`uname -s`
        if [ "$os" = "Linux" ]; then
          checksum=`md5sum $1 | awk '{ print $1 }'`
        elif [ "$os" = "Darwin" ]; then
          checksum=`cat $1 | md5`
        fi
        if [ "$checksum" = "$2" ]; then
          echo "Checksum is correct. No need to download $1."
          return 1
        else
          echo "Checksum is incorrect. Need to download again $1"                
        fi
    else
      return 0
    fi
  else 
    echo invalid argument:
    echo $*
    exit -1
  fi
}
# 从github上下载源码,如果本地不存在指定的zip包,或md5校验码不匹配则从github下载
# $1 项目名称
# $2 版本号
# $3 zip文件的md5
# $4 项目所有者名称
# $5 zip包文件名前缀
download_from_github(){
  if [ $# -eq 5 ]
  then
    project_folder="$1-$2"
    project_package="$project_folder.zip"
    if need_download $project_package $3
    then
      echo "(下载源码)downloading $1 $2 source"
      wget --no-check-certificate https://github.com/$4/$1/archive/$5$2.zip -O $project_package
      exit_on_error
    fi
    remove_if_exist $project_folder
    unzip $project_package
    exit_on_error
  else
    echo invalid argument:
    echo $*
    exit -1
  fi
}

download_from_github "protobuf" "3.3.1" "9377e414994fa6165ecb58a41cca3b40" "google" "v"

popd

protobuf编译安装

对下载的protobuf源码编译并安装到release/protobuf_linux_x86_64文件夹下
build_protobuf.sh

#!/bin/bash
GXX_PATH="-DCMAKE_BUILD_TYPE:STRING=RELEASE"
INSTALL_FOLDER=$(dirname $(readlink -f $0))/release/protobuf_linux_x86_64
pushd protobuf-3.3.1
echo INSTALL_FOLDER:$INSTALL_FOLDER
if [ -d build.gcc ]
then 
	rm -fr build.gcc/*
else 
	mkdir build.gcc
fi
pushd build.gcc
cmake ../cmake $GXX_PATH -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$INSTALL_FOLDER  -Dprotobuf_BUILD_TESTS=off
# 编译并安装protobuf到指定的文件夹
make install
popd
rm -fr build.gcc
popd

上面的脚本中在调用cmake生成Makefile时定义了protobuf_BUILD_TESTS=off,这是因为protobuf的CMakeLists.txt中默认定义protobuf_BUILD_TESTS=on
而当protobuf_BUILD_TESTS=on时,需要gmock支持(https://github.com/google/googlemock),否则cmake会报如下错误:

CMake Error at tests.cmake:2 (message):
Cannot find gmock directory.

gmock是google公司推出的一款开源的白盒测试工具,对于我们一般的应用来说,不需要gmock提供的功能,所以这里可以将protobuf_BUILD_TESTS=off,避免麻烦。

在Windows平台上编译Protobuf,你需要按照以下步骤进行操作: 1. 首先,你需要下载Protobuf的源代码。你可以从官方网站或者GitHub上找到最新版本的源代码。 2. 解压下载的源代码文件,并进入解压后的文件夹。 3. 在Windows操作系统上编译Protobuf需要使用CMake工具。你需要先安装CMake,并确保将CMake的可执行文件路径添加到系统的环境变量中。 4. 打开命令提示符(cmd)或者PowerShell,并进入到Protobuf源代码的根目录。 5. 创建一个用于构建的目录,例如"build"。 6. 进入构建目录,并执行以下命令来生成构建脚本: ``` cmake -G "Visual Studio 15 2017 Win64" .. ``` 这里使用的是Visual Studio 2017的生成器,你可以根据你的实际情况选择适合的生成器版本。 7. 执行以下命令来开始编译Protobuf: ``` cmake --build . --config Release ``` 这将会在Release模式下编译Protobuf库。 8. 编译完成后,你将会在构建目录中找到生成的Protobuf库文件(.lib文件)和头文件。你可以将这些文件添加到你的项目中,并在代码中使用Protobuf。 请注意,上述步骤仅仅是编译Protobuf库,如果你想使用Protobuf来编写和解析消息,你还需要安装相应的Protobuf编译器(protoc)并生成相应的代码文件。 希望这些步骤可以帮助你在Windows平台上成功编译Protobuf。如果你有其他问题,请随时提问。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [protobuf编译安装和简单使用C++(Windows+VS平台)](https://blog.csdn.net/dhl11/article/details/124748506)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Windows系统编译protobuf方法及实例](https://download.csdn.net/download/dodouaj/9496099)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值