【Protobuf】( ubuntu18/20/22 ) apt | 源码安装卸载protobuf、CMakeLists指定多版本protobuf某一特定版本

apt安装

(ubuntu20.04默认3.6.0、ubuntu18.04默认3.0.0)

sudo apt-get install libprotobuf-dev protobuf-compiler

tips protoc --version查看已安装版本


源码编译安装

  • 安装依赖

sudo apt install autoconf automake libtool curl g++ gcc
  • 下载源码(最好在gihub上下载)

下载自己想要的对应版本(这里以3.10.0为例)

https://github.com/protocolbuffers/protobuf/tags

  • 解压进入安装工程目录

tar -zxvf protobuf-3.10.0.tar.gz
cd protobuf-3.10.0
  • 生成配置文件

此步骤若报错,参见下文错误总结。

./autogen.sh
  • 指定安装路径(多版本安装很重要)

    • 如果你已经apt默认安装了系统稳定版本,项目所需另一版本,如20.04 apt默认安装了3.6.0,现在安装3.10.0
    • 两种做法:
      1. 卸载已有版本(卸载方式参见下文);
      2. 多版本共存(指定安装路径
# ./configure --prefix=$INSTALL_DIR,此处如下
# 在这里指定/usr/local/protobuf3.10
./configure --prefix=/usr/local/protobuf3.10

注: ./configure 不指定选项即为默认安装路径,与apt路径一致,若已通过apt安装其他版本,需要先卸载,以免发生头文件、库文件冲突

./configure
  • 编译安装

# 多线程快速编译(取决于你的服务器,超过数目会以系统自身的最大值编译)
make -j20 -l20
# (时间超级长,一般完全可以不检查,大部分是warnning信息)
make check 
# 安装
sudo make install
  • 刷新动态库

sudo ldconfig
  • 配置环境变量

# 打开
sudo gedit /etc/profile
# 文末尾加入(把bin路径和pkgconfig路径添加到系统PATH)
export PATH=$PATH:/usr/local/protobuf3.10/bin/
export PKG_CONFIG_PATH=/usr/local/protobuf3.10/lib/pkgconfig/

# 这一步是必须的,因为如果少了这一步,会出现找不到protoc的命令错误
source /etc/profile

# 再次刷新动态库
sudo ldconfig
  • 配置动态链接库

# 打开
sudo gedit /etc/ld.so.confcd
# 文末尾加入
/usr/local/protobuf3.10/lib

# 再次刷新动态库
sudo ldconfig
  • 创建软连接

sudo ln -s /usr/local/protobuf3.10/bin/protoc /usr/local/bin/protoc3.6

至此安装完毕啦!!!

  • 快查看你的版本并编译一个proto文件吧

protoc3.10 --version
# 将当前目录下的proto编译到当前文件夹下
proro3.10  lidarPerceptionMsg.proto planningMsg.proto controllerMsg.proto --cpp_out=./

注: protoc --version 或者 which protoc 失败怎么办???

😺​ 重启系统reboot 重新载入环境即可

源码编译安装时出现的问题

./autogen.sh 生成配置文件出现的问题
Google Mock not present Fetching gmock-1.7.0 from the web…/autogen.sh: 34: curl: not found

# (未安装gmock)下载gmock
git clone https://github.com/paulsapps/gmock-1.7.0.git
# 把gmock解压出来的目录拷贝到protobuf-3.10.0安装目录下,改名为gmock(源文件没有gmock)
mv gmock-1.7.0 gmock

# 打开配置文件
sudo gedit autogen.sh
# 注释如下代码
if test ! -e gmock; then
  echo "Google Mock not present.  Fetching gmock-1.7.0 from the web..."
  curl $curlopts -O https://googlemock.googlecode.com/files/gmock-1.7.0.zip
  unzip -q gmock-1.7.0.zip
  rm gmock-1.7.0.zip
  mv gmock-1.7.0 gmock
fi

protobuf卸载

# 卸载aptan安装的protobuf
sudo dpkg -l | grep protobuf
sudo apt-get remove libprotobuf-dev protobuf-compiler
# 卸载源码
which protoc
# 输出路径
/usr/local/protobuf/bin/protoc
rm /usr/local/bin/protoc

CMakeLists文件编写===>>>选取对应版本头、库文件

# 定义查找路径
set(Protobuf_PREFIX_PATH "/usr/local/protobuf3.10")
# 添加到 CMAKE_PREFIX_PATH
list(APPEND CMAKE_PREFIX_PATH ${Protobuf_PREFIX_PATH})
# 查找 Protobuf
find_package(Protobuf REQUIRED)
include_directories(
	    ${Protobuf_INCLUDE_DIR}
)
target_link_libraries(project ${PROTOBUF_LIBRARIES})

🐑 🐑 🐑 🐑 🐑 🐑 🐑 🐑 🐑 🐑 🐑 🐑🐑 🐑🐑 🐑🐑 🐑🐑 🐑

最近更新

  1. TCP协议是安全的吗?

    2023-12-13 15:32:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-13 15:32:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-13 15:32:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-13 15:32:01       18 阅读

热门阅读

  1. Babylonjs学习笔记(十)——拉伸多边形

    2023-12-13 15:32:01       33 阅读
  2. 名称空间与函数对象

    2023-12-13 15:32:01       35 阅读
  3. 工具:Jupyter

    2023-12-13 15:32:01       38 阅读
  4. 力扣面试150题 | 209.长度最小的子数组

    2023-12-13 15:32:01       34 阅读
  5. 工厂模式实现

    2023-12-13 15:32:01       40 阅读
  6. 力扣labuladong——一刷day70

    2023-12-13 15:32:01       39 阅读
  7. POJ:1113

    2023-12-13 15:32:01       41 阅读
  8. springboot全局异常处理和自定义异常处理

    2023-12-13 15:32:01       40 阅读
  9. 轻松应用字典树

    2023-12-13 15:32:01       41 阅读