centos 安装protobuf (string和byes 格式区别)

本文探讨了Google Protocol Buffers中bytes和string的区别。虽然两者在C++接口下都表现为std::string,但string会有UTF-8格式检查,而bytes则没有。为了提高效率,建议在明确编码格式后直接使用bytes,避免不必要的UTF-8验证。这是一个关于protobuf高效使用的技巧分享。

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

麻烦大家去官网先下载

Download Protocol Buffers  |  Google Developers 从官网跳到github

Release Protocol Buffers v3.18.0 · protocolbuffers/protobuf · GitHub

下载

protobuf-cpp-3.18.0.tar.gz

tar -xvf protobuf-cpp-3.18.0.tar.gz

cd protobuf-3.18.0/
./configure -prefix=/usr/local/
make

make install

protoc --version

bytes 和 string 区别

  1. protobuf里的string/bytes在C++接口里实现上都是std::string
  2. 两者序列化、反序列化格式上一致,不过对于string格式,会有一个utf-8格式的检查。

出于效率,我们应当在确定字段编码格式后直接使用bytes,减少utf8编码的判断,效率上会有提高。

#include <iostream>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/compiler/importer.h>
#include <google/protobuf/io/coded_stream.h>

using namespace google::protobuf;
using namespace google::protobuf::compiler;

int main() {
    // 初始化 Protocol Buffers 库
    GOOGLE_PROTOBUF_VERIFY_VERSION;

    // 创建一个Importer对象来处理.proto文件
    Importer importer("", nullptr);

    // 加载指定的proto文件
    const std::string filename = "person.proto";
    const FileDescriptor* fileDesc = importer.pool()->FindFileByName(filename);

    if (fileDesc != nullptr) {
        std::cout << "成功找到文件: " << filename << std::endl;

        // 获取文件的所有消息类型
        for (int i = 0; i < fileDesc->message_type_count(); i++) {
            const Descriptor* messageType = fileDesc->message_type(i);
            std::cout << "消息类型: " << messageType->name() << std::endl;
        }
    } else {
        std::cerr << "未能找到文件: " << filename << std::endl;
    }

    // 清理资源
    google::protobuf::ShutdownProtobufLibrary();

    return 0;
}

方法二直接immport:

#include <iostream>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/compiler/importer.h>
#include <google/protobuf/io/coded_stream.h>

using namespace google::protobuf;
using namespace google::protobuf::compiler;

int main() {
    // 初始化 Protocol Buffers 库
    GOOGLE_PROTOBUF_VERIFY_VERSION;

    // 创建 Importer 对象来处理 .proto 文件
    Importer importer("", nullptr);

    // 导入 msg.proto 文件
    const std::string filename = "msg.proto";
    const FileDescriptor* fileDesc = importer.Import(filename);

    if (fileDesc != nullptr) {
        std::cout << "成功找到文件: " << filename << std::endl;

        // 获取文件中的所有消息类型
        for (int i = 0; i < fileDesc->message_type_count(); i++) {
            const Descriptor* messageType = fileDesc->message_type(i);
            std::cout << "消息类型: " << messageType->name() << std::endl;
        }
    } else {
        std::cerr << "未能找到文件: " << filename << std::endl;
    }

    // 清理资源
    google::protobuf::ShutdownProtobufLibrary();

    return 0;
}

 

要在CentOS安装使用protobuf,可以按照以下步骤进行操作: 1. 首先,确保系统中已经安装了autoreconf工具。如果没有安装,可以使用以下命令安装: ``` sudo yum install autoconf automake libtool ``` 2. 接下来,使用wget命令下载protobuf安装包。例如,下载版本为3.21.9的protobuf安装包,可以使用以下命令: ``` wget https://github.com/protocolbuffers/protobuf/releases/download/v3.21.9/protobuf-cpp-3.21.9.tar.gz ``` 3. 解压下载的安装包,并进入解压后的目录: ``` tar -xvzf protobuf-cpp-3.21.9.tar.gz cd protobuf-3.21.9/ ``` 4. 运行autogen.sh脚本以生成配置文件: ``` ./autogen.sh ``` 5. 执行configure命令以配置protobuf安装路径: ``` ./configure ``` 6. 运行make命令进行编译: ``` make ``` 7. 使用sudo make install命令以管理员权限安装protobuf: ``` sudo make install ``` 8. 安装完成后,可以使用protoc --version命令来验证安装是否成功,并查看protobuf的版本号: ``` protoc --version ``` 以上是在旧版本的CentOS安装protobuf的步骤。如果你使用的是新版本的CentOS,可以按照以下步骤进行操作: 1. 下载protobuf安装包并解压: ``` tar zvxf protobuf-all-3.6.1.tar.gz cd protobuf-3.6.1 ``` 2. 运行configure命令进行配置。你可以选择添加--prefix选项来指定安装路径: ``` ./configure --prefix=/usr/local/ ``` 3. 执行make命令进行编译。这个过程可能需要一些时间: ``` make ``` 4. 使用sudo make check命令运行测试: ``` sudo make check ``` 5. 最后,使用sudo make install命令以管理员权限安装protobuf: ``` sudo make install ``` 安装完成后,可以使用protoc --version命令来验证安装是否成功,并查看protobuf的版本号: ``` protoc --version ``` 通过按照以上步骤安装protobuf,你就可以在CentOS上成功安装使用它了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值