CMake#
The BSR generates C++ SDKs and serves them as content FetchContent can pull into a CMake project at configure time.
CMake 3.14 or later is required (for FetchContent_MakeAvailable).
For an end-to-end walkthrough across SDK languages, see the generated SDKs quickstart.
Before you start#
- Use CMake 3.14 or later.
- Sign in to the BSR with
buf registry login. This writes credentials to~/.netrc, which theNETRC REQUIREDdirective on eachFetchContent_Declareconsumes. For the full setup, see Authentication. - Configure-time network access has to reach the BSR;
FetchContentresolves the SDK URL duringcmake -B build, not at compile time.
Minimal CMakeLists.txt#
Fetch googleapis_googleapis_protocolbuffers_cpp and link it into a target:
cmake_minimum_required(VERSION 3.14)
project(my_project CXX)
include(FetchContent)
FetchContent_Declare(googleapis_googleapis_protocolbuffers_cpp
URL https://buf.build/gen/cmake/googleapis/googleapis/protocolbuffers/cpp/v26.1-8bc2c51e08c4.1
NETRC REQUIRED
EXCLUDE_FROM_ALL
)
FetchContent_MakeAvailable(googleapis_googleapis_protocolbuffers_cpp)
add_executable(my_project src/main.cpp)
target_link_libraries(my_project PRIVATE googleapis_googleapis_protocolbuffers_cpp)
For multi-SDK projects or any project that doesn’t keep FetchContent calls inline in CMakeLists.txt, see Organize multiple SDKs below.
Where the URL comes from#
The BSR generates a copy-paste-ready FetchContent_Declare snippet for each SDK on the module’s SDKs tab (for example, the googleapis/googleapis SDKs page at https://buf.build/googleapis/googleapis/sdks).
Open the page, pick the C++ plugin, and copy the snippet from the CMake tab.
For programmatic construction, the URL is:
https://buf.build/gen/cmake/{moduleOwner}/{moduleName}/{pluginOwner}/{pluginName}/{pluginVersion}-{commitHash}.{pluginRevision}
The commitHash is the first 12 characters of the module commit ID.
pluginRevision is Buf’s revision counter for that plugin version, separate from pluginVersion; it can change while the plugin’s own version stays the same.
Library naming#
The BSR derives one name from the module and plugin:
Use that exact string in three places:
FetchContent_Declare’s first argument.FetchContent_MakeAvailable’s argument.target_link_libraries.
The library name comes from the downloaded archive, so don’t substitute a different identifier (the CMake file name in the example below, or any of the URL path pieces).
Organize multiple SDKs#
For more than a couple of SDKs, factor each FetchContent_Declare into its own file under cmake/ and include() it from the main CMakeLists.txt:
FetchContent_Declare(googleapis_googleapis_protocolbuffers_cpp
URL https://buf.build/gen/cmake/googleapis/googleapis/protocolbuffers/cpp/v26.1-8bc2c51e08c4.1
NETRC REQUIRED
EXCLUDE_FROM_ALL
)
FetchContent_MakeAvailable(googleapis_googleapis_protocolbuffers_cpp)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(googleapis_googleapis_protocolbuffers_cpp)
target_link_libraries(my_project PRIVATE googleapis_googleapis_protocolbuffers_cpp)
Use the BSR-chosen library name as the file’s basename (without the .cmake extension) so include() resolves it through CMAKE_MODULE_PATH.
Available plugins#
The BSR supports two plugins for CMake:
protocolbuffers/cppfor Protobuf message types.grpc/cppfor gRPC service stubs.
For how those plugins are packaged, see the bufbuild/plugins repository; to request a new plugin, file an issue.
Troubleshooting#
include could not find requested file#
CMake’s include() first looks for an exact-path match relative to CMAKE_SOURCE_DIR, then searches CMAKE_MODULE_PATH for a file with a .cmake extension.
The snippets above assume CMAKE_MODULE_PATH is set, which is why include() doesn’t pass .cmake.
If your project doesn’t set CMAKE_MODULE_PATH, add the cmake/ directory to it:
Or pass the explicit relative path with the extension: