Linux环境下Protobuf完整安装和使用教程

Linux环境下Protobuf完整安装和使用教程

原文地址:https://blog.csdn.net/yahohi/article/details/108310609

下载和安装

1、下载protobuf安装包

$ git clone https://github.com/protocolbuffers/protobuf.git

2、安装依赖库

$ cd protobuf/
$ ./autogen.sh
$ ./configure --prefix=/usr/local/protobuf
$ make
$ sudo make install
$ sudo ldconfig                  // 刷新共享库,很重要的一步

3、检查安装是否成功

$ protoc --version
环境配置

1、添加环境变量

sudo vim /etc/profile

添加:

export PATH=$PATH:/usr/local/protobuf/bin/
export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/

2、然后,刷新环境变量:

source /etc/profile

3、按照上述方式修改~/.profile。

4、配置动态链接库路径

sudo vim /etc/ld.so.conf

添加:

/usr/local/protobuf/lib
一个例子
1、报文定义
syntax = "proto3";
message SearchResponse {
  message Result {
    string url = 1;
    string title = 2;
    int64  telephone = 3;
    repeated string snippets = 4;
  }
  repeated Result results = 1;
}
2、报文生成

执行如下命令:

protoc --cpp_out=./ testproto.proto

就会在该目录下生成如下文件:

testproto.pb.h
testproto.pb.cc
3、使用报文
#include <iostream>
#include "testproto.pb.h"
#include <string>
using namespace std;

int main()
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;
    SearchResponse sr;
    
    SearchResponse_Result* result = sr.add_results();
    result->set_url("url ...");
    
    string str = sr.SerializeAsString();
    std::cout << str << endl;
    return 0;
}
4、编译程序
g++ -g -I/usr/local/ -I/usr/local/protobuf/ -I/usr/local/bin/ -I/usr/local/protobuf/include/google/protobuf/ -I/usr/local/protobuf/include/ -std=c++11 -MMD -MP -MF -lprotobuf -pthread -o testproto testproto.pb.o testproto.pb.cc

特别注意, -lprotobuf -pthread一定要添加!

5、常见问题:

问题1:

对‘google::protobuf::internal::VerifyVersion(int, int, char const*)’未定义的引用

解决方案:
在编译命令中添加**-lprotobuf**选项。

问题2:
编译通过,但运行时报错:

terminate called after throwing an instance of 'google::protobuf::FatalException'
  what():  CHECK failed: (scc->visit_status.load(std::memory_order_relaxed)) == (SCCInfoBase::kRunning): 

解决方案:
在编译命令中添加** -pthread**选项。

  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
安装protobuf,您可以按照以下步骤进行操作: 对于Windows系统: 1. 下载protobuf,并解压缩文件。 2. 配置环境变量,将protobuf安装路径添加到系统的PATH变量中。 3. 使用cmd命令行窗口,输入"protoc --version",以检查protobuf是否成功安装。如果能够显示protobuf的版本信息,则表示安装成功。 对于Linux系统: 1. 下载protobuf,并解压缩文件。 2. 执行"autogen.sh"命令来生成配置脚本。如果您下载的是特定语言的protobuf版本,则不需要执行该步骤。 3. 执行"configure"命令来配置protobuf安装。有两种方式可选: - 默认安装在"/usr/local"目录下,但库文件和可执行文件是分散的。执行命令"./configure"。 - 修改安装目录,将protobuf统一安装在"/usr/local/protobuf"下。执行命令"./configure --prefix=/usr/local/protobuf"。 4. 编译和安装protobuf,执行"make"和"make install"命令。 在Linux下执行"protoc --version"命令,以检查protobuf是否成功安装。如果能够显示protobuf的版本信息,则表示安装成功。 请根据您所使用的操作系统,选择对应的步骤进行安装。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [protobuf安装教程](https://blog.csdn.net/Djsnxbjans/article/details/131904947)[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_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值