参考下文配置Protobuf:
https://github.com/protocolbuffers/protobuf/tree/master/src#c-installation—windows
所需环境
推荐提前安装以下环境:
- Windows 10
- Git
- Cmake
- Visual Studio 2019
生成工具
在https://github.com/protocolbuffers/protobuf/releases/latest下载protobuf生成工具。
文件应形如protoc-$VERSION-win32.zip,解压其中的bin/protoc.exe,放于Dep/protobuf/bin下。
编译依赖
参考下文编译所需依赖:
https://github.com/protocolbuffers/protobuf/blob/master/cmake/README.md
在https://github.com/protocolbuffers/protobuf/releases/latest下载protobuf源文件。
文件应形如 protobuf-cpp-[VERSION].tar.gz。
解压全部文件到文件夹中,此处以D:\Protobuf为例。
打开cmake-gui,选择source code文件夹为D:\Protobuf\cmake,build the binaries文件夹为目标文件夹,此处以D:\Protobuf-build为例。
点击Configure,选择Visual Studio 2019。
取消配置中的所有勾选。
留意CMAKE_INSTALL_PREFIX值,此值为之后运行安装后目标文件存放位置。此处以D:/Protobuf-install为例。
点击Generate生成解决方案,之后Open Project打开解决方案。
选择MinSizeRel与x64,之后生成解决方案。
右键INSTALL,选择生成,目标文件会被生成在前面所配置文件夹中。
复制D:/Protobuf-install/include到项目/protobuf/include。
复制D:/Protobuf-install/lib到项目/protobuf/lib。
配置项目
在Visual Studio的项目中,参考如下方法配置Protobuf。
配置头文件引用
右键项目属性,点击C/C++,在附加包含目录之中,填入protobuf头文件目录,如:\protobuf\include。
配置静态链接
右键项目属性,点击链接器,在附加库目录之中,填入protobuf依赖文件目录,如:\protobuf\lib。
展开链接器一栏,选择输入,在附加依赖项之中,填入protobuf依赖文件名,如libprotobuf.lib与libprotobuf-lite.lib。