blob: 43939580c068aac2fdd4adc0d1fcc2e199b40935 [file] [log] [blame] [view]
Zack Galbreath684aa482019-12-12 18:55:021# gRPC C++
Jayant Kolhe80949e32015-02-19 19:27:072
Zack Galbreath684aa482019-12-12 18:55:023This directory contains the C++ implementation of gRPC.
Jayant Kolhe80949e32015-02-19 19:27:074
Jan Tattermuschf389e522018-06-12 15:26:315# To start using gRPC C++
Yuchen Zeng341b5ab2016-03-09 01:16:296
Zack Galbreath684aa482019-12-12 18:55:027This section describes how to add gRPC as a dependency to your C++ project.
8
Jan Tattermuschf389e522018-06-12 15:26:319In the C++ world, there's no universally accepted standard for managing project dependencies.
10Therefore, gRPC supports several major build systems, which should satisfy most users.
11
Yash Tibrewal1040c1b2021-08-17 20:58:3512## Supported Platforms
13
Esun Kimb1b59eb2022-06-30 20:22:5914* Officially Supported: These platforms are officially supported. We follow
15 [the OSS Foundational C++ Support Policy](https://opensource.google/documentation/policies/cplusplus-support)
16 to choose platforms to support.
17 We test our code on these platform and have automated continuous integration tests for
AJ Hellerf5b3a492021-10-05 02:05:5218 them.
Esun Kimb1b59eb2022-06-30 20:22:5919 .
Yash Tibrewal1040c1b2021-08-17 20:58:3520
Nathan Baulchb2a05be2024-10-10 22:35:3421* Best Effort: We do not have continuous integration tests for these, but we are
AJ Hellerf5b3a492021-10-05 02:05:5222 fairly confident that gRPC C++ would work on them. We will make our best
23 effort to support them, and we welcome patches for such platforms, but we
24 might need to declare bankruptcy on some issues.
25
26* Community Supported: These platforms are supported by contributions from the
27 open source community, there is no official support for them. Breakages on
28 these platforms may go unnoticed, and the community is responsible for all
29 maintenance. Unmaintained code for these platforms may be deleted.
Yash Tibrewal1040c1b2021-08-17 20:58:3530
Esun Kim8df210e2025-06-04 19:47:0631| Operating System | Architectures | Support Level |
32|--------------------------------|---------------|----------------------|
33| Linux - Debian, Ubuntu, CentOS | x86, x64 | Officially Supported |
34| Windows 10+ | x86, x64 | Officially Supported |
35| MacOS | x64, ARM64 | Officially Supported |
36| Linux - Others | x86, x64 | Best Effort |
37| Linux | ARM64 | Best Effort |
38| iOS | | Best Effort |
39| Android | | Best Effort |
40| AIX | | Community Supported |
41| Asylo | | Community Supported |
42| FreeBSD | | Community Supported |
43| Fuchsia | | Community Supported |
44| NaCL | | Community Supported |
45| NetBSD | | Community Supported |
46| OpenBSD | | Community Supported |
47| Solaris | | Community Supported |
Yash Tibrewal1040c1b2021-08-17 20:58:3548
Zack Galbreath684aa482019-12-12 18:55:0249## Bazel
Jan Tattermuschf389e522018-06-12 15:26:3150
Esun Kim8df210e2025-06-04 19:47:0651Bazel is the gRPC core development team's main build system.
52It offers speedy builds and effortlessly manages dependencies that already support Bazel.
Jan Tattermuschf389e522018-06-12 15:26:3153
Esun Kim8df210e2025-06-04 19:47:0654To add gRPC as a Bazel dependency:
55
561. Find your desired gRPC version on the
57 [Bazel Central Registry](https://registry.bazel.build/modules/grpc).
582. Then, use [bazel_dep](https://bazel.build/rules/lib/globals/module#bazel_dep)
59 to add the gRPC dependency to your `MODULE.bazel` file.
60
61```
62bazel_dep(name = "grpc", version = "1.72.0")
63```
Jan Tattermuschf389e522018-06-12 15:26:3164
Zack Galbreath684aa482019-12-12 18:55:0265## CMake
Jan Tattermusch48b1f8d2019-11-28 14:42:4466
Zack Galbreath684aa482019-12-12 18:55:0267`cmake` is your best option if you cannot use bazel. It supports building on Linux,
68MacOS and Windows (official support) but also has a good chance of working on
69other platforms (no promises!). `cmake` has good support for crosscompiling and
70can be used for targeting the Android platform.
Jan Tattermusch48b1f8d2019-11-28 14:42:4471
Zack Galbreath684aa482019-12-12 18:55:0272To build gRPC C++ from source, follow the [BUILDING guide](../../BUILDING.md).
Jan Tattermusch48b1f8d2019-11-28 14:42:4473
Esun Kim02fb4b52025-01-10 20:48:1874To ensure all libraries in your CMake project compile with the same C++ version
75(e.g., C++17), explicitly specify the standard:
76
77```cmake
78set(CMAKE_CXX_STANDARD 17)
79set(CMAKE_CXX_STANDARD_REQUIRED ON)
80```
81
82This configuration enforces the use of C++17 for all targets and avoids potential
83inconsistencies or errors due to different C++ versions being used.
84
Zack Galbreath684aa482019-12-12 18:55:0285### find_package
Jan Tattermuschf389e522018-06-12 15:26:3186
Zack Galbreath684aa482019-12-12 18:55:0287The canonical way to discover dependencies in CMake is the
88[`find_package` command](https://cmake.org/cmake/help/latest/command/find_package.html).
89
90```cmake
91find_package(gRPC CONFIG REQUIRED)
92add_executable(my_exe my_exe.cc)
93target_link_libraries(my_exe gRPC::grpc++)
94```
95[Full example](../../examples/cpp/helloworld/CMakeLists.txt)
96
97`find_package` can only find software that has already been installed on your
98system. In practice that means you'll need to install gRPC using cmake first.
99gRPC's cmake support provides the option to install gRPC either system-wide
100(not recommended) or under a directory prefix in a way that you can later
101easily use it with the `find_package(gRPC CONFIG REQUIRED)` command.
102
103The following sections describe strategies to automatically build gRPC
104as part of your project.
105
106### FetchContent
107If you are using CMake v3.11 or newer you should use CMake's
108[FetchContent module](https://cmake.org/cmake/help/latest/module/FetchContent.html).
109The first time you run CMake in a given build directory, FetchContent will
110clone the gRPC repository and its submodules. `FetchContent_MakeAvailable()`
111also sets up an `add_subdirectory()` rule for you. This causes gRPC to be
112built as part of your project.
113
114```cmake
Esun Kima49d4502024-09-30 17:52:26115cmake_minimum_required(VERSION 3.16)
Zack Galbreath79f1a072020-03-06 19:56:01116project(my_project)
117
Zack Galbreath684aa482019-12-12 18:55:02118include(FetchContent)
119FetchContent_Declare(
120 gRPC
121 GIT_REPOSITORY https://github.com/grpc/grpc
Jan Tattermuschd99b5412020-03-17 09:53:37122 GIT_TAG RELEASE_TAG_HERE # e.g v1.28.0
Zack Galbreath684aa482019-12-12 18:55:02123)
Zack Galbreath79f1a072020-03-06 19:56:01124set(FETCHCONTENT_QUIET OFF)
Zack Galbreath684aa482019-12-12 18:55:02125FetchContent_MakeAvailable(gRPC)
126
127add_executable(my_exe my_exe.cc)
128target_link_libraries(my_exe grpc++)
129```
130
Zack Galbreath79f1a072020-03-06 19:56:01131Note that you need to
132[install the prerequisites](../../BUILDING.md#pre-requisites)
133before building gRPC.
134
Zack Galbreath684aa482019-12-12 18:55:02135### git submodule
136If you cannot use FetchContent, another approach is to add the gRPC source tree
137to your project as a
138[git submodule](https://git-scm.com/book/en/v2/Git-Tools-Submodules).
139You can then add it to your CMake project with `add_subdirectory()`.
140[Example](../../examples/cpp/helloworld/CMakeLists.txt)
141
Zack Galbreath684aa482019-12-12 18:55:02142### Support system-installed gRPC
143
144If your project builds gRPC you should still consider the case where a user
145wants to build your software using a previously installed gRPC. Here's a
146code snippet showing how this is typically done.
147
148```cmake
149option(USE_SYSTEM_GRPC "Use system installed gRPC" OFF)
150if(USE_SYSTEM_GRPC)
151 # Find system-installed gRPC
152 find_package(gRPC CONFIG REQUIRED)
153else()
154 # Build gRPC using FetchContent or add_subdirectory
155endif()
156```
157
158[Full example](../../examples/cpp/helloworld/CMakeLists.txt)
159
160## pkg-config
161
162If your project does not use CMake (e.g. you're using `make` directly), you can
163first install gRPC C++ using CMake, and have your non-CMake project rely on the
164`pkgconfig` files which are provided by gRPC installation.
165[Example](../../test/distrib/cpp/run_distrib_test_cmake_pkgconfig.sh)
166
Esun Kima4b43cc2021-07-15 16:45:01167**Note for CentOS 7 users**
168
169CentOS-7 ships with `pkg-config` 0.27.1, which has a
170[bug](https://bugs.freedesktop.org/show_bug.cgi?id=54716) that can make
171invocations take extremely long to complete. If you plan to use `pkg-config`,
172you'll want to upgrade it to something newer.
173
Zack Galbreath684aa482019-12-12 18:55:02174## make (deprecated)
Jan Tattermuschf389e522018-06-12 15:26:31175
Jan Tattermusch48b1f8d2019-11-28 14:42:44176The default choice for building on UNIX based systems used to be `make`, but we are no longer recommending it.
177You should use `bazel` or `cmake` instead.
Jan Tattermuschf389e522018-06-12 15:26:31178
179To install gRPC for C++ on your system using `make`, follow the [Building gRPC C++](../../BUILDING.md)
180instructions to build from source and then install locally using `make install`.
181This also installs the protocol buffer compiler `protoc` (if you don't have it already),
182and the C++ gRPC plugin for `protoc`.
183
184WARNING: After installing with `make install` there is no easy way to uninstall, which can cause issues
185if you later want to remove the grpc and/or protobuf installation or upgrade to a newer version.
186
Jan Tattermuschf389e522018-06-12 15:26:31187## Packaging systems
188
Jan Tattermusch942e4682019-10-29 17:04:16189We do not officially support any packaging system for C++, but there are some community-maintained packages that are kept up-to-date
190and are known to work well. More contributions and support for popular packaging systems are welcome!
191
192### Install using vcpkg package
193gRPC is available using the [vcpkg](https://github.com/Microsoft/vcpkg) dependency manager:
194
195```
196# install vcpkg package manager on your system using the official instructions
197git clone https://github.com/Microsoft/vcpkg.git
198cd vcpkg
Yan Reznikov39d007e2021-06-11 09:21:49199
200# Bootstrap on Linux:
Jan Tattermusch942e4682019-10-29 17:04:16201./bootstrap-vcpkg.sh
Yan Reznikov39d007e2021-06-11 09:21:49202# Bootstrap on Windows instead:
203# ./bootstrap-vcpkg.bat
204
Jan Tattermusch942e4682019-10-29 17:04:16205./vcpkg integrate install
206
207# install gRPC using vcpkg package manager
Yan Reznikov39d007e2021-06-11 09:21:49208./vcpkg install grpc
Jan Tattermusch942e4682019-10-29 17:04:16209```
210
211The gRPC port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please [create an issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository.
Jan Tattermuschf389e522018-06-12 15:26:31212
213
214## Examples & Additional Documentation
Yuchen Zeng341b5ab2016-03-09 01:16:29215
Yuchen Zeng798b0dc2016-03-09 20:00:09216You can find out how to build and run our simplest gRPC C++ example in our
Yuchen Zengd85ef062016-03-18 18:05:25217[C++ quick start](../../examples/cpp).
Yuchen Zeng341b5ab2016-03-09 01:16:29218
Yuchen Zeng798b0dc2016-03-09 20:00:09219For more detailed documentation on using gRPC in C++ , see our main
Mehrdad Afsharibb3d95b2017-07-10 22:24:28220documentation site at [grpc.io](https://grpc.io), specifically:
Yuchen Zeng798b0dc2016-03-09 20:00:09221
Patrice Chalinb8514ea2020-03-14 01:47:06222* [Overview](https://grpc.io/docs): An introduction to gRPC with a simple
Yuchen Zeng798b0dc2016-03-09 20:00:09223 Hello World example in all our supported languages, including C++.
Patrice Chalin5ac3aa72020-06-16 12:47:24224* [gRPC Basics - C++](https://grpc.io/docs/languages/cpp/basics):
Yuchen Zeng798b0dc2016-03-09 20:00:09225 A tutorial that steps you through creating a simple gRPC C++ example
226 application.
Patrice Chalin5ac3aa72020-06-16 12:47:24227* [Asynchronous Basics - C++](https://grpc.io/docs/languages/cpp/async):
Yuchen Zeng798b0dc2016-03-09 20:00:09228 A tutorial that shows you how to use gRPC C++'s asynchronous/non-blocking
229 APIs.
230
231
Jan Tattermuschf389e522018-06-12 15:26:31232# To start developing gRPC C++
Yuchen Zeng798b0dc2016-03-09 20:00:09233
Jan Tattermuschf389e522018-06-12 15:26:31234For instructions on how to build gRPC C++ from source, follow the [Building gRPC C++](../../BUILDING.md) instructions.