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

目标

本文将教会一个完全的新手如何搭建自己的protobuf Linux环境,并配置好编程环境,并使用一个例子帮助你学会使用protobuf.

下载和安装

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**选项。

Linux环境下使用Protobuf的步骤如下: 1. 首先,确保你已经安装了libprotobuf库和protoc编译器。一般来说,libprotobuf库位于/usr/local/lib路径下,而protoc一般位于/usr/local/bin路径下。 2. 在你的代码中,引入所需的头文件,例如#include "testproto.pb.h"。然后,根据你的需求使用Protobuf的报文。你可以根据自己的需求创建和操作Protobuf的对象。 3. 在编译程序之前,确保你已经设置了正确的编译选项和路径。为了正确地编译程序,你需要指定正确的头文件路径和库文件路径。例如,你可以使用以下命令编译程序: ```bash 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 ``` 4. 在运行程序之前,确保你已经设置了LD_LIBRARY_PATH环境变量,将libprotobuf库的路径添加到其中。你可以使用以下命令设置LD_LIBRARY_PATH环境变量: ```bash export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib ``` 5. 如果你需要在命令行中直接运行protoc编译器,你可以将protoc所在目录添加到PATH环境变量中,并将libprotobuf的pkgconfig目录添加到PKG_CONFIG_PATH环境变量中。例如,你可以使用以下命令添加PATH和PKG_CONFIG_PATH环境变量: ```bash export PATH=$PATH:/usr/local/protobuf/bin/ export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/ ``` 通过以上步骤,你就可以在Linux环境下成功使用Protobuf了。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值