How to support Debug and Release builds of grpc under Windows

Viewed 492

Followed the grpc build from source documentation and installed a system-wide build of grpc using the default Win32 Release target. When compiling a client/server in Debug mode I get the linker mismatches (MD_DynamicRelease doesn't match MDd_DynamicDebug).

However, when I build and install a Win32 Debug target, the installer overwrites the release libraries with the same name, and thus I get the same problem in reverse with all the Release client/server code failing to link.

What is the proper way to create and deploy both Release and Debug builds of grpc?

1 Answers

I'm facing the same problem, and I see at least two possible solutions:

  1. Use the git submodule (or, almost equivalently, the FetchContent method), i.e. add the grpc code directly into your CMakeLists.txt via an add_subdirectory command. It's the easiest way, because CMake will link the approprate C Runtime Library based on the current build type; however you'll find all the grpc code and targets included in your project tree;

  2. Build and install grpc twice, once in Release (or RelWithDebInfo if you prefer) and once in Debug mode, targeting two different install folders. Something like this (I'm using Ninja as build system and MSVC as compiler):

    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 .
    

    Thereafter, you can link the two different versions independently, for Debug and Release builds of your project. Using CMake you will need to set the CMAKE_PREFIX_PATH variable to the location of the Debug or the Release grpc install folder you've just built. If you are using the CMake integration in Visual Studio, you can add the proper definition of the CMAKE_PREFIX_PATH variable in the CMakeSettings.json file, like this

    {
      "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"
        }
      ]
    }
    

Note that if your project doesn't use CMake as build system, you have to adapt the second part of solution 2. according to your build system (i.e. set the proper include directories, library references and protoc prebuild step).

Related