【原创】centos环境下google protobuf的安装和使用

centos 环境安装google protobuf2.6.1-artifacts:

https://github.com/protocolbuffers/protobuf/tree/2.6.1-artifacts下载版本,解压:
1.执行./autogen.sh
  + mkdir -p third_party/googletest/m4
+ autoreconf -f -i -Wall,no-obsolete
configure.ac:104: error: possibly undefined macro: AC_PROG_LIBTOOL
      If this token and others are legitimate, please use m4_pattern_allow.
      See the Autoconf documentation.
autoreconf: /usr/bin/autoconf failed with exit status: 1

2.1中报错由于缺少autoconf,autoconf,libtool,sudo yum install automake autoconf libtool,再次重复1,成功。
   手动下载地址:http://ftp.gnu.org/gnu/

3../configure  成功
4.make 成功
6.make check //提示没有第三方软件包,本身就没有第三方软件代码,失败跳过
7.make install
8../configure --prefix=/usr
9.敲入命令protoc,产生各种命令提示,表示安装成功。


10.创建proto文件:cmdntest.proto
  内容为:
syntax = "proto2";
package demo;

message OMM_TO_HSS_REQ_T {
    required int32 nReqType = 1;
    required int32 nFunction = 2;
    optional int32 nUserID = 3;
    optional int32 nQryFlag = 4;
    optional int32 nFlag = 5;
    optional int32 nRecIndex = 6;
    optional int32 nRecBeginIndex = 7;
    optional int32 nRecEndIndex = 8;
    repeated PROTOBUF_STRUCT_ALL_T stStructAll = 9;

}

message PROTOBUF_STRUCT_ALL_T {
    repeated PROTOBUF_CMDN_TEST_T stMdnTest = 1;

}


message PROTOBUF_CMDN_TEST_T {
    optional string cMdn = 1;

}

11.执行命令 protoc --cpp_out=. cmdntest.proto
提示:protoc: error while loading shared libraries: libprotoc.so.10: cannot open shared object file: No such file or directory
查看export 查看LD_LIBRARY_PATH,把编译好的.src/.lib路径加入其中。
vim /etc/profile,加入export LD_LIBRARY_PATH="/home/echat/lib:/zte/libs:/home/zhuwei0216000866/protobuf-3.0.x/protobuf-3.0.x/src/.libs
12.生成如下文件:
   cmdntest.pb.cc
   cmdntest.pb.h
13.使用protobuf进行消息编解码:

14.编译增加makefile


####################################################
#将所在目录下的所有C源文件,编译成一个可执行文件
#实际使用时基本修改下配置参数部分即可
####################################################

#1 查找.c源文件,并定义同名.o文件
#makefile有隐晦规则,自动会将.c文件编译成.o目标文件
SRCS :=cmdtestmain.cpp cmdntest.pb.cc
OBJS :=cmdtestmain.o cmdntest.pb.o

####################################################
#2 配置参数
#TARGET 目标名 必须设置
#CC 编译工具链名称
#LIBS 库文件 
#    用相对路径或者绝对路径均可,多个库文件用空格
#    隔开,没有留空
#LDFLAGS 链接库文件标记,没有留空
#INCLUDE 头文件搜索目录 
#    使用-I开头,如-I./include,表示要到
#    ./include目录下搜索头文件,多个目录,用空格
#    隔开,没有留空
#DEFINES 自定义选项,如-D"DEBGU",定义宏"DEBUG"
#CFLAGS 编译选项,发布版本,可以去除-g,减小程序
####################################################
TARGET  :=cmdtestmain
CC      :=g++
LIBS    :=./protobuf-3.0.x/protobuf-3.0.x/src/.libs/libprotobuf.so
LDFLAGS :=
DEFINES :=
INCLUDE :=-I ./protobuf-3.0.x/protobuf-3.0.x/src/google/protobuf
CFLAGS  :=-std=gnu++11 -g -Wall -O3 -lm $(DEFINES) $(INCLUDE)

#3 规则 规则部分最好不用修改
.PHONY : all clean rebuild

all: $(TARGET)  

$(TARGET):$(OBJS)
    $(CC) $(CFLAGS) -o $(TARGET) $^ $(LDFLAGS) $(LIBS)

rebuild: clean all

clean:
    rm -rf $(OBJS)
    rm -rf $(TARGET)

    
    
15.make报错:error This file requires compiler and library support for the ISO C++ 2011 standard
   解决方法:makefile中CFLAG增加:-std=gnu++11
   
   make报错:
   In file included from cmdntest.pb.cc:4:0:
   cmdntest.pb.h:17:2: 错误:#error This file was generated by an older version of protoc which is。
   
   解决方法:更换更高prototbuf版本,protobuf3.0.x问题解决。
   

   
16.make编译

make

g++    -c -o cmdtestmain.o cmdtestmain.cpp
g++    -c -o cmdntest.pb.o cmdntest.pb.cc
g++ -std=gnu++11 -g -Wall -O3 -lm  -I ./protobuf-3.0.x/protobuf-3.0.x/src/google/protobuf -o cmdtestmain cmdtestmain.o cmdntest.pb.o  ./protobuf-3.0.x/protobuf-3.0.x/src/.libs/libprotobuf.so

    


   
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值