本文简单介绍如何根据 protobuf 的 .proto 文件生成 C++ 使用的头文件和源文件。
参考:protobuf
目录
ProtoBuf 通过 proto 文件生成 cc/h 文件的步骤如下:
编译 protoc 可执行文件
Linux 端
先通过 protobuf 源文件编译出 protoc 可执行文件:
git clone https://github.com/protocolbuffers/protobuf.git
cd protobuf/
git checkout -b v2.6.1 origin/2.6.1-artifacts
wget https://codeload.github.com/google/googletest/tar.gz/release-1.5.0
tar -zxvf release-1.5.0
mv googletest-release-1.5.0/ gtest
./autogen.sh
./configure
make -j8
sudo make install
Mac 端
直接利用 brew 安装 protobuf :
brew search protobuf
brew install protobuf@3
安装完之后根据提示将环境变量添加到 ~/.bash_profile
中,然后就可以使用 protoc 生成 cc/h 文件了。
利用 protoc 生成 cc/h 文件
- 然后通过 proto 文件生成 cc/h 文件:
cd src
./protoc --proto_path=. --cpp_out=. ./Test.proto
如果 make install
失败,则直接使用全路径的 protoc 生成 cc/h 文件:
cd protobuf
./src/protoc --proto_path=. --cpp_out=. ./Test.proto