Mac安装Protobuf 3.14.0

1、Pom依赖:

<!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java -->
<dependency>
    <groupId>com.google.protobuf</groupId>
    <artifactId>protobuf-java</artifactId>
    <version>3.14.0</version>
</dependency>

2、GitHub下载:

image.png

3、解压

image.png

4、安装

4.1 进入目录

cd protobuf-3.14.0/

4.2 设置编译目录

./configure --prefix=/usr/local/protobuf
image.png

4.3 切换到root用户

sudo -i

4.4 安装

make
make install
image.png

4.5 配置环境变量

vim .bash_profile

添加

export PROTOBUF=/usr/local/protobuf 
export PATH=$PROTOBUF/bin:$PATH

source一下使文件生效

source .bash_profile

4.6 查看protoc版本

protoc --version
image.png

4.7 编写proto文件

syntax="proto3"; // //这个版本的protoc的protobuf编译器已经可以支持proto2语法和proto3的语法
package model;
option java_outer_classname = "StudentModel";//输出的类名
message Student{
   int32 id = 1;
   string name = 2;
}

4.8 protoc 编译

image.png

4.9 pb测试

   public static void main(String[] args) {
        // 消息体构构建器
        StudentModel.Student.Builder builder = StudentModel.Student.newBuilder();
        builder.setId(100);
        builder.setName("你猜我猜不猜");

        // builder转对象
        StudentModel.Student student = builder.build();
        System.out.println(student.getName());
        System.out.println(student.getId());
        System.out.println(student.toByteArray().length);

    }
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容