编译protobuf库生成库文件等
✅ 一、前提准备
确保你已经安装了以下工具:
CMake ≥ 3.15
Visual Studio (VS2019 或 VS2022),含命令行工具
Git(可选)
已下载并解压的 protobuf-3.19.6 源码
假设源码路径为:
C:\src\protobuf-3.19.6
✅ 二、设置环境(使用 VS 的命令行环境)
打开:
x64 Native Tools Command Prompt for VS 2022
(如果你是 32 位就用对应的)
✅ 三、生成 Debug 版本 (MDd) 的库
1. 创建构建目录
cd C:\src\protobuf-3.19.6
mkdir build_debug
cd build_debug
2. 用 CMake 生成工程
cmake -G "Visual Studio 17 2022" -A x64 ^
  -DCMAKE_BUILD_TYPE=Debug ^
  -Dprotobuf_BUILD_TESTS=OFF ^
  -Dprotobuf_MSVC_STATIC_RUNTIME=OFF ^
  -Dprotobuf_BUILD_SHARED_LIBS=ON ^
  -DCMAKE_INSTALL_PREFIX=C:\src\protobuf-3.19.6\install_debug ^
  ..
说明:
选项	含义
-DCMAKE_BUILD_TYPE=Debug	生成 Debug 配置
-Dprotobuf_MSVC_STATIC_RUNTIME=OFF	让 Protobuf 使用 /MDd 而不是 /MTd
-Dprotobuf_BUILD_SHARED_LIBS=ON	生成 DLL 和 LIB
-Dprotobuf_BUILD_TESTS=OFF	跳过测试,加快编译
-DCMAKE_INSTALL_PREFIX=...	安装路径
3. 编译和安装
cmake --build . --config Debug --target install
执行完后,会在:
C:\src\protobuf-3.19.6\install_debug\
生成:
bin\*.dll
lib\*.lib
include\google\protobuf\*.h
✅ 四、验证运行时选项 (MDd)
可以用 dumpbin 验证生成的 DLL 是否链接 /MDd:
dumpbin /directives C:\src\protobuf-3.19.6\build_debug\libprotobufd.lib | findstr /C:"RuntimeLibrary"
或者直接在 Qt 项目中链接时,VS 会显示:
Linking against MSVCRTD (Debug DLL Runtime)
这表明是 MDd。
✅ 五、Qt 工程中使用
在 Qt .pro 文件中添加:
INCLUDEPATH += C:/src/protobuf-3.19.6/install_debug/include
LIBS += -LC:/src/protobuf-3.19.6/install_debug/lib -llibprotobufd
(注意:Debug 版本的库通常名字带 d)
✅ 六、额外说明(如果你也想要 Release)
你也可以用同样的命令生成 Release 版本,只要改:
mkdir build_release
cd build_release
cmake -G "Visual Studio 17 2022" -A x64 ^
  -DCMAKE_BUILD_TYPE=Release ^
  -Dprotobuf_BUILD_TESTS=OFF ^
  -Dprotobuf_MSVC_STATIC_RUNTIME=OFF ^
  -Dprotobuf_BUILD_SHARED_LIBS=ON ^
  -DCMAKE_INSTALL_PREFIX=C:\src\protobuf-3.19.6\install_release ^
  ..
cmake --build . --config Release --target install
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号