Skip to content

Instantly share code, notes, and snippets.

@chobie
Created January 14, 2014 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chobie/8420185 to your computer and use it in GitHub Desktop.
Save chobie/8420185 to your computer and use it in GitHub Desktop.
PROJECT(ex)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
INCLUDE(FindProtobuf)
FIND_PACKAGE(Protobuf REQUIRED)
FILE(GLOB ProtoFiles "${CMAKE_CURRENT_SOURCE_DIR}/*.proto")
PROTOBUF_GENERATE_CPP(ProtoSources ProtoHeaders ${ProtoFiles})
FILE(GLOB SRC_EX *.cc)
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
ADD_EXECUTABLE(ex ${SRC_EX} ${ProtoSources})
TARGET_LINK_LIBRARIES(ex ${PROTOBUF_LIBRARIES})
#include <iostream>
#include "person.pb.h"
int main()
{
Person p, p2;
std::string result;
p.set_name("Chobie");
p.SerializeToString(&result);
std::string const& s = static_cast<const std::string &>(result);
p2.ParseFromString(s);
std::cout << p2.name() << std::endl;
std::cout << p2.phone().number() << std::endl;
return 0;
}
message Person
{
optional string name = 1;
message Phone
{
optional string number = 1 [default = "12345"];
}
optional Phone phone = 2;
}
@chobie
Copy link
Author

chobie commented Jan 14, 2014

mkdir build && cd build && cmake .. && make
./ex

macbook% ./ex
Chobie
12345

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment