We’re excited to announce that in addition to C#, Go, JavaScript/TypeScript, Java/Kotlin, Python, Swift, and Rust, the Buf Schema Registry now provides generated SDKs for C++ via CMake. The BSR's CMake repository currently serves generated SDKs for the widely used protocolbuffers/cpp and grpc/cpp plugins.
How we did it
Generated SDKs for C++ contain everything you need to seamlessly integrate Protobuf into your CMake project.
However, CMake is a build system and not a package manager, so we had to approach them differently than usual.
You can use CMake’s FetchContent
command to get the generated C++ libraries directly from the BSR.
Like our other generated SDKs, the BSR resolves all transitive module and plugin dependencies automatically for you. There’s no need to navigate the dependencies of your code or use multiple C++ plugins to get the correct generated types you need. You can depend on a Protobuf module like any other C++ library in your CMake project.
Example usage
You can integrate BSR-hosted Protobuf modules into your project with a few short steps. The BSR helpfully illustrates them, providing you with the exact code snippets you need. However, here’s a quick example using the googleapis module on the BSR.
Create a FetchContent
script
First, let’s create a CMake script using the FetchContent
command to get the generated SDK from the BSR:
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)
The URL points to a unique location which provides a zip file containing the generated C++ code, including additional CMake configuration
files that make it easy to integrate the code into your project.
Add the script to your build process
Next, let’s invoke the script as part of your build process, using CMake's include
command. In your main CMakeLists.txt
file, add the following line:
include(googleapis_googleapis_protocolbuffers_cpp)
Link the library
Finally, link the downloaded library into your project using the target_link_libraries
CMake command:
target_link_libraries(
<yourProjectName>
...
googleapis_googleapis_protocolbuffers_cpp
)
And that's it! Now, you can begin to depend on the SDK in your project. For example:
#include "google/rpc/status.pb.h"
#include <iostream>
int main(int argc, char *argv[]) {
google::rpc::Status status;
status.set_code(42);
std::cout << status.code();
return 0;
}
We're excited for the Buf Schema Registry to start supporting the C++ ecosystem. Get in touch on our Slack channel or shoot us an email at feedback@buf.build with questions and suggestions!