【问题标题】:How to support Debug and Release builds of grpc under Windows如何在 Windows 下支持 grpc 的 Debug 和 Release 构建
【发布时间】:2020-09-19 01:05:55
【问题描述】:

遵循源文档中的 grpc 构建,并使用默认的 Win32 发布目标安装了系统范围的 grpc 构建。在调试模式下编译客户端/服务器时,链接器不匹配(MD_DynamicRelease 与 MDd_DynamicDebug 不匹配)。

但是,当我构建和安装 Win32 调试目标时,安装程​​序会覆盖具有相同名称的发布库,因此我遇到了相同的问题,所有发布客户端/服务器代码都无法链接。

创建和部署 grpc 的 Release 和 Debug 版本的正确方法是什么?

【问题讨论】:

    标签: debugging grpc release


    【解决方案1】:

    我也面临同样的问题,我看到了至少两种可能的解决方案:

    1. 使用git submodule(或几乎等同于FetchContent 方法),即通过add_subdirectory 命令将grpc 代码直接添加到您的CMakeLists.txt 中。这是最简单的方法,因为 CMake 会根据当前构建类型链接适当的 C 运行时库;但是您会发现项目树中包含的所有 grpc 代码和目标;

    2. 构建和安装 grpc 两次,一次在 Release(或 RelWithDebInfo,如果您愿意)和一次在 Debug 模式下,针对两个不同的安装文件夹。像这样的东西(我使用 Ninja 作为构建系统和 MSVC 作为编译器):

      rem Run from grpc directory after cloning the repo with --recursive or updating submodules.
      call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\vcvars64.bat"
      cd cmake
      rem Building x64 Release
      mkdir build_x64_Release
      cd build_x64_Release
      cmake ..\.. -GNinja -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=..\..\install\x64\Release
      cmake --build .
      cmake --install .
      rem Building x64 Debug
      cd ..
      mkdir build_x64_Debug
      cd build_x64_Debug
      cmake ..\.. -GNinja -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=..\..\install\x64\Debug
      cmake --build .
      cmake --install .
      

      此后,您可以独立链接两个不同的版本,用于项目的调试和发布版本。使用 CMake,您需要将 CMAKE_PREFIX_PATH 变量设置为您刚刚构建的 Debug 或 Release grpc 安装文件夹的位置。如果您在 Visual Studio 中使用 CMake 集成,则可以在 CMakeSettings.json 文件中添加 CMAKE_PREFIX_PATH 变量的正确定义,如下所示

      {
        "configurations": [
          {
            "name": "x64-Debug",
            "generator": "Ninja",
            "configurationType": "Debug",
            "inheritEnvironments": [ "msvc_x64_x64" ],
            "buildRoot": "${projectDir}\\out\\build\\${name}",
            "installRoot": "${projectDir}\\out\\install\\${name}",
            "cmakeCommandArgs": "-DCMAKE_PREFIX_PATH=C:\\Dev\\grpc\\install\\x64\\Debug"
          },
          {
            "name": "x64-Release",
            "generator": "Ninja",
            "configurationType": "RelWithDebInfo",
            "inheritEnvironments": [ "msvc_x64_x64" ],
            "buildRoot": "${projectDir}\\out\\build\\${name}",
            "installRoot": "${projectDir}\\out\\install\\${name}",
            "cmakeCommandArgs": "-DCMAKE_PREFIX_PATH=C:\\Dev\\grpc\\install\\x64\\Release"
          }
        ]
      }
      

    请注意,如果您的项目不使用 CMake 作为构建系统,则必须根据您的构建系统调整解决方案 2. 的第二部分(即设置正确的包含目录、库引用和 protoc 预构建步骤)。

    【讨论】:

      猜你喜欢
      • 2021-06-09
      • 2017-06-14
      • 2015-06-25
      • 1970-01-01
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多