c++grpcunresolved-externalgrpc-c++

GRPC C++ build/linker issues on Windows


I'm on day three of trying to get a "simple" grpc application going but have hit the 15th roadblock.

So far I've:

However when I try to build my application I'm bombarded with errors related to unresolved external symbols enter image description here

From my understanding of the error, I'm not linking to something that contains each of the symbols missing however given I've

Its been a long time since I've touched c++ so appologies if this is a simple issue


Solution

  • After some more messing around I managed to get things working. @sweenish got me on the right track with suggesting using cmake and pointing me to https://github.com/grpc/grpc/tree/master/src/cpp however I did still need to build from source (which given https://grpc.io/docs/languages/cpp/quickstart/ starts with building GRPC from source, I believe is the intended way to go.)

    Given I found so little useful info on how to end-to-end get a basic c++ GRPC app running, I figured I'd answer my question with a step by step of how to get things working so the next person has an easier time.

    Building GRPC on Windows

    These steps are similar to those mentioned in https://grpc.io/docs/languages/cpp/quickstart/ however they will be targeting Windows

    1. Pick a directory you'd like to install GRPC into and ensure it exists, I'll be using C:\libs.
    2. Install the tools referenced in https://github.com/grpc/grpc/blob/master/BUILDING.md#windows
    3. Clone the GRPC repo git clone --recurse-submodules -b v1.60.0 --depth 1 --shallow-submodules https://github.com/grpc/grpc
    4. cd into the grpc/cmake directory. Make a new directory called build and cd into it. You should now be in grpc/cmake/build where grpc is the root directory of the repo you cloned
    5. Initalise cmake, passing in the location you'll be installing GRPC to. cmake -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF "-DCMAKE_INSTALL_PREFIX=C:/libs" ../.. . Note the file path / is the worng way around for Windows (no idea if this matters but it works)
    6. Build GRPC with cmake --build . --config Release - Go make a coffee, even on a high end machine this can take upwards of 30 mins. You may have some luck with cmake --build . --config Release -- /m speeding things up
    7. Install GRPC with cmake --install . --config Release
    8. If you want a Debug version then redo the previous step with --config Debug

    If everything worked, GRPC should be installed in your directory of choice. An easy way to check is to go to [INSTALL_DIR]\bin and run protoc.exe --version, if you get something like libprotoc 25.0 then you've successfully built GRPC for Windows.

    Compiling Applications with GRPC on Windows

    Compiling GRPC is half the battle, now we need to build an app that uses it to confirm everything is working. A good test is the HelloWorld app that comes as part of the repo you checked out in the previous step located at grpc\examples\cpp\helloworld. I'll go through both commandline and Visual Studio building as they are slightly different.

    Command Line

    1. Change directory to grpc\examples\cpp\helloworld
    2. Initalise cmake with cmake "-DCMAKE_PREFIX_PATH=C:/libs" . . Make sure you replace C:/libs with your install location
    3. Build the project with cmake --build . --config Release

    The output directory structure is a bit of a mess but you should now have a helloworld server and client application at the following locations. Give them both a run to confirm everything is working

    Visual Studio 2022

    Not sure if its needed but I'll mention it just incase, In Visual Studio Installer I have the default options for Desktop development with C++ installed which includes C++ CMake tools for Windows.

    1. Open Visual Studio and select Open a local folder
    2. Open grpc\examples\cpp\helloworld
    3. Click the configuration dropdown (currently set to x64-Debug) and select Manage Configurations...
    4. In the CMake command arguments section add "-DCMAKE_PREFIX_PATH=C:/libs" (including the quotes)
    5. When you save, cmake will regenerate its cache.
    6. In the Solution Explorer, click the purple icon (Switch between available views) to switch to the cmake targets view
    7. Build and run a target

    Hopefully all that helps someone oneday, time to start actually using GRPC with C++!

    EDIT: I recently stumbled on a far easier method for compiling GRPC for usage with Visual Studio and given this method doesn't require the usage of cmake for your end project, it is far nicer.

    Building GRPC via Visual Studio

    1. Clone the GRPC repo git clone --recurse-submodules -b v1.60.0 --depth 1 --shallow-submodules https://github.com/grpc/grpc
    2. Make a new directory under the grps repo and change to it mkdir grpc/mybuilddir cd grpc/mybuilddir
    3. Run cmake -G "Visual Studio 17 2022" -A x64 -DCMAKE_SYSTEM_VERSION=10.0.22621.0 .. . This process took around a minute for me
    4. Launch Visual Studio as admin (the next step involves installing GRPC into Program Files which requires administrative rights)
    5. Right click on the INSTALL project and select build
    6. Coffee time! This will take around half an hour
    7. If all goes well, GRPC should be installed to C:\Program Files\grpc

    Using GRPC in a new project

    Importing GRPC into a new c++ project isn't intuitive but the following steps should get you close.

    1. On your c++ project, set the following values

    Configuration Properties -> c/c++ -> General -> Additional Directories = C:\Program Files\grpc\include

    Configuration Properties -> Linker -> General -> Additional Library Dependencies = C:\Program Files\grpc\lib

    Configuration Properties -> Linker -> Command Line -> Additional Options = "C:\Program Files\grpc\lib\*.lib" <- Possibly not the best way but GRPC has so many .lib files that this was the easiest option

    Thats it, you should be ready to build some protobug files, import them and use GRPC