-
下載 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 兩個編譯好的文件,復制到你的開發工程即可使用。