google protobuf

官网

安装

vs2015 编译 google protobuf 3.5.1

下载:

protobuf 3.5.1
cmake

编译

使用 VS2015开发人员命令提示:

进入 protobuf 的目录:
首先创建一个 building文件夹,并创建tmp,x86_debug子文件夹:

VS2015开发人员命令提示进入上面的 tmp目录,执行:

cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Debug  -Dprotobuf_BUILD_TESTS=OFF  -Dprotobuf_BUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=../x86_debug ../../cmake

等待cmake generate结束后,执行:

nmake 
nmake install

如果想要使用 release 版本,只需要更改上述的命令行为:-DCMAKE_BUILD_TYPE=Release
查看一下版本号,到bin文件目录下执行:

.protoc.exe --version 
libprotoc 3.5.1

VS2015 工程环境配置

配置包含目录和库目录

增加预处理器定义

增加连接器依赖

禁用SDL检查

不使用预编译头

添加dll路径

用下试试

新建文件:addressbook.proto

syntax = "proto2";
package tutorial;

message Person {
    required string name = 1;
    required int32 id = 2;
    optional string email = 3;

    enum PhoneType {
        MOBILE = 0;
        HOME = 1;
        WORK = 2;
    }

    message PhoneNumber {
        required string number = 1;
        optional PhoneType type = 2 [default = HOME];
    }

    repeated PhoneNumber phones = 4;
}

message Addressbook {
    repeated Person people = 1;
}

在bin目录下使用命令行:

.protoc.exe --cpp_out=. .addressbook.proto

生成文件:

将上面的的文件复制到 VS2015 的工程当中,
写文件:

#include <iostream>
#include <fstream>
#include <string>
#include "addressbook.pb.h"

using namespace std;
void PromptForAddress(tutorial::Person* person) {
	cout << "Enter person ID number: ";
	int id;
	cin >> id;
	person->set_id(id);
	cin.ignore(256, '
');

	cout << "Enter name: ";
	getline(cin, *person->mutable_name());

	cout << "Enter email address (blank for none): ";
	string email;
	getline(cin, email);
	if (!email.empty())
		person->set_email(email);

	while (true) {
		cout << "Enter a phone number (or leave blank for finish): ";
		string number;
		getline(cin, number);
		if (number.empty())
			break;

		tutorial::Person::PhoneNumber* phone_number = person->add_phones();
		phone_number->set_number(number);

		cout << "Is this a mobile, home, or work phone? ";
		string type;
		getline(cin, type);
		if (type == "mobile")
			phone_number->set_type(tutorial::Person::MOBILE);
		else if (type == "home")
			phone_number->set_type(tutorial::Person::HOME);
		else if (type == "work")
			phone_number->set_type(tutorial::Person::WORK);
		else
			cout << "Unkown phone type. Using default. " << endl;
	}
}


int main(int argc, char* argv[]) {
	GOOGLE_PROTOBUF_VERIFY_VERSION;

	if (argc != 2) {
		cerr << "Usage: " << argv[0] << "ADDRESS_BOOK_FILE " << endl;
		return -1;
	}

	tutorial::Addressbook address_book;

	fstream input(argv[1], ios::in | ios::binary);
	if (!input)
		cout << argv[1] << ": File not found. Creating a new file. " << endl;
	else if (!address_book.ParseFromIstream(&input)) {
		cerr << "Failed to parse address book. " << endl;
		return -1;
	}

	PromptForAddress(address_book.add_people());

	fstream output(argv[1], ios::out | ios::trunc | ios::binary);
	if (!address_book.SerializeToOstream(&output)) {
		cerr << "Failed to write address book. " << endl;
		return -1;
	}

	google::protobuf::ShutdownProtobufLibrary();
	return 0;
}

读文件:

#include <iostream>
#include <fstream>
#include <string>
#include "addressbook.pb.h"

using namespace std;


void ListPeople(const tutorial::Addressbook& address_book) {
	for (int i = 0; i < address_book.people_size(); ++i) {
		const tutorial::Person& person = address_book.people(i);

		cout << "Person ID: " << person.id() << endl;
		cout << "Name: " << person.name() << endl;
		if (person.has_email()) {
			cout << " E-mail address: " << person.email() << endl;
		}

		for (int j = 0; j < person.phones_size(); ++j) {
			const tutorial::Person::PhoneNumber& phone_number = person.phones(j);
			switch (phone_number.type()) {
			case tutorial::Person::MOBILE:
				cout << " Mobile phone#: ";
				break;
			case tutorial::Person::HOME:
				cout << " Home phone#: ";
				break;
			case tutorial::Person::WORK:
				cout << " Work phone#: ";
				break;
			}
			cout << phone_number.number() << endl;
		}
	}
}

int main(int argc, char** argv) {
	GOOGLE_PROTOBUF_VERIFY_VERSION;
	if (argc != 2) {
		cerr << "Usage: " << argv[0] << "ADDRESS_BOOK_FILE " << endl;
		return -1;
	}

	tutorial::Addressbook address_book;
	fstream input(argv[1], ios::in | ios::binary);
	if (!address_book.ParseFromIstream(&input)) {
		cerr << "Failed to parse address book. " << endl;
		return -1;
	}

	ListPeople(address_book);

	google::protobuf::ShutdownProtobufLibrary();
	return 0;
}

VS2015 加入命令行参数的方法:

原文地址:https://www.cnblogs.com/xiaojianliu/p/13329122.html