I'm on day three of trying to get a "simple" grpc application going but have hit the 15th roadblock.
So far I've:
C:\libs\grpc\grpc
.cc
and .h
files using protoc
and added them to my project
Include
and Library
directories
However when I try to build my application I'm bombarded with errors related to unresolved external symbols
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
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.
These steps are similar to those mentioned in https://grpc.io/docs/languages/cpp/quickstart/ however they will be targeting Windows
C:\libs
.git clone --recurse-submodules -b v1.60.0 --depth 1 --shallow-submodules https://github.com/grpc/grpc
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 clonedcmake -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)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 upcmake --install . --config Release
--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 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.
grpc\examples\cpp\helloworld
cmake "-DCMAKE_PREFIX_PATH=C:/libs" .
. Make sure you replace C:/libs
with your install locationcmake --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
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
.
Open a local folder
grpc\examples\cpp\helloworld
x64-Debug
) and select Manage Configurations...
CMake command arguments
section add "-DCMAKE_PREFIX_PATH=C:/libs"
(including the quotes)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.
git clone --recurse-submodules -b v1.60.0 --depth 1 --shallow-submodules https://github.com/grpc/grpc
mkdir grpc/mybuilddir
cd grpc/mybuilddir
cmake -G "Visual Studio 17 2022" -A x64 -DCMAKE_SYSTEM_VERSION=10.0.22621.0 ..
. This process took around a minute for meINSTALL
project and select buildC:\Program Files\grpc
Importing GRPC into a new c++ project isn't intuitive but the following steps should get you close.
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