GRPC C++ build/linker issues on Windows

187 Views Asked by At

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

2

There are 2 best solutions below

0
On BEST ANSWER

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

  • grpc\examples\cpp\helloworld\Release\greeter_server.exe
  • grpc\examples\cpp\helloworld\Release\greeter_client.exe

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++!

0
On

It seems that your project missed required linker dependencies such as grpc and grpc++. You can specify it manually or let cmake do it for you.