google protobuf在ubuntu上安装使用步骤

本文详细介绍如何从GitHub下载并安装Protobuf,包括必要的依赖项安装、生成安装脚本及配置动态链接库路径等步骤。此外,还介绍了如何进行交叉编译及使用Protobuf将消息文件映射为C++文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、从github下载protobuff源码。


     https://github.com/google/protobuf

2、安装依赖项:

  * autoconf
  * automake
  * libtool
  * curl (used to download gmock)

执行下面即可:

  $ sudo apt-get install autoconf automake libtool curl


3、产生安装脚本:

     $ ./autogen.sh    ##注意:此处需要翻墙才能成功
   

4、安装:

    $ cd protobuf-master 
    $ ./configure --prefix=/usr/local/protobuf   ##这里是定义安装的位置
    $ make
    $ make check
    $ sudo make install
    $ sudo ldconfig # refresh shared library cache.

5、配置动态链接库路径

     sudo vim /etc/ld.so.conf

      插入:
     /usr/local/protobuf/lib变量。

    sudo vim /etc/profile
 添加
   export PATH=$PATH:/usr/local/protobuf/bin/
   export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/
   保存执行
   source /etc/profile


6、如果要交叉编译的话,比如我要编译hi3518上面用的库时,./configure的时候这样设置
     ./configure --build=i686-pc-linux  --host=arm-hisiv100nptl-linux --with-protoc=protoc --disable-shared --prefix=/home/huang/Desktop/thridparty/protobuf/2.5.0/forHaisi

然后make&&make install 就可以了

写消息文件:msg.proto
package lm; 
message helloworld 
{ 
    required int32     id = 1;  // ID   
    required string    str = 2;  // str  
    optional int32     opt = 3;  //optional field 
}
将消息文件msg.proto映射成cpp文件
protoc -I=. --cpp_out=. msg.proto
可以看到生成了
msg.pb.h 和msg.pb.cc

----------------------------------结束------------------------------------------




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值