很久没有写C++程序了,很多东西都忘了,几天试了一下protobuf的,顺便写了Makefile:
根据./proto/目录下的addressbook.proto( 内容请参考)生成.cc和.h文件,放到./cpp/proto/目录下
$ protoc -I=./proto/ --cpp_out=./cpp/proto/ ./proto/addressbook.proto
在 ./cpp/ 目录下
$ cd ./cpp
编写reader.cc 和 writer.cc( 内容请参考 )
编写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.txtEnter person ID number: 01Enter name: donalEnter email address (blank for none): donal0412@gmail.comEnter a phone number (or leave blank to finish): 88236017Is this a mobile, home, or work phone? workEnter 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