windows11 编译 protobuf-3.21.12 c++
-
下载 protobuf 包,本文使用 3.21.12 版本,Gitub下载链接: Github官网 , 网盘下载: 网盘
-
如果电脑环境没有安装 cmake 则需要安装,本文测试使用 cmake-3.25.1 版本,
下载地址:[camke-3.25.1] (https://pan.baidu.com/s/11W8Xv-2h49JWHQF5zF9gRQ?pwd=1111)
双击 cmake-3.25.1-windows-x86_64.msi 安装。 -
解压 protobuf-cpp-3.21.12.tar.gz 到当前路径,进入 protobuf-3.21.12 文件夹,创建 cmake 文件夹。
-
进入 cmake 文件夹,右键打开cmd窗口
(1) Debug 模式执行以下命令:cmake -A x64 -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=install -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_MSVC_STATIC_RUNTIME=OFF -DBUILD_SHARED_LIBS=OFF ../cmake cmake --build . --config Debug --target install
生成的文件在 install 文件夹下,复制所有文件到你的开发工程即可使用。
(2) Release 模式执行以下命令:
cmake -A x64 -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=install_release -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_MSVC_STATIC_RUNTIME=OFF -DBUILD_SHARED_LIBS=OFF ../cmake cmake --build . --config Release --target install
生成的文件在 install_release 文件夹下,复制所有文件到你的开发工程即可使用。
-
使用 protoc 编译 proto文件,例如:复制待编译 test.proto 文件到 protobuf-3.21.12\cmake\install_release\bin 文件夹下,打开cmd窗口,执行以下命令:
.\protoc.exe --cpp_out=./ test.proto
执行完成后会在当前目录生成 test.pb.cc 和 test.pb.h 两个编译好的文件,复制到你的开发工程即可使用。