protobuf 测试 & Makefile example
很久没有写C++程序了,很多东西都忘了,几天试了一下protobuf的tutorial,顺便写了Makefile:
根据./proto/目录下的addressbook.proto( 内容请参考tutorial)生成.cc和.h文件,放到./cpp/proto/目录下
$ protoc -I=./proto/ --cpp_out=./cpp/proto/ ./proto/addressbook.proto
在 ./cpp/ 目录下
$ cd ./cpp
编写reader.cc 和 writer.cc( 内容请参考tutorial )
编写Makefile文件,关于gcc编译过程和makefile中的特殊符号,请参考这里和这里
$ cat Makefile
LIBS=-lprotobuf
CC=gcc
ARGS=-Wall -c
all:reader writer
reader:reader.o addressbook.pb.o
$(CC) $^ $(LIBS) -o $@
writer:writer.o addressbook.pb.o
$(CC) $^ $(LIBS) -o $@
reader.o:reader.cc proto/addressbook.pb.h
$(CC) $(ARGS) $*.cc -o $@
writer.o:writer.cc proto/addressbook.pb.h
$(CC) $(ARGS) $*.cc -o $@
addressbook.pb.o:proto/addressbook.pb.cc proto/addressbook.pb.h
$(CC) $(ARGS) proto/$*.cc -o $@
clean:
rm *.o -f
rm reader writer -f
生成reader和writer程序
$make
测试 writer和reader
$ ./writer address.txt Enter person ID number: 01 Enter name: donal Enter email address (blank for none): donal0412@gmail.com Enter a phone number (or leave blank to finish): 88236017 Is this a mobile, home, or work phone? work Enter a phone number (or leave blank to finish):
$ ./reader address.txt Person ID: 1 Name: donal E-mail address: donal0412@gmail.com Work phone #: 88236017