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