Skip to content

C++ Language Support

Status: ✅ Full Support
Examples:

C++ support includes the complete Protocol Buffer ecosystem with gRPC, CMake integration, and embedded-friendly alternatives like nanopb.

PluginDescriptionGenerated Files
protocBase message generation*.pb.cc, *.pb.h
grpc_cpp_plugingRPC service generation*.grpc.pb.cc, *.grpc.pb.h
PluginDescriptionGenerated Files
nanopbEmbedded C/C++ (minimal RAM)*.pb.c, *.pb.h
protobuf-cPure C code generation*.pb-c.c, *.pb-c.h
languages.cpp = {
enable = true;
outputPath = "gen/cpp";
standard = "c++17";
optimizeFor = "SPEED";
};
languages.cpp = {
enable = true;
outputPath = "gen/cpp";
# Protobuf version selection
protobufVersion = "latest"; # Options: "3.21", "3.25", "3.27", "4.25", "5.29", "latest"
# C++ standard and optimization
standard = "c++17"; # Options: "c++17", "c++20", "c++23"
optimizeFor = "SPEED"; # Options: "SPEED", "CODE_SIZE", "LITE_RUNTIME"
runtime = "full"; # Options: "full", "lite"
# Build system integration
cmakeIntegration = true;
pkgConfigIntegration = true;
# Performance options
arenaAllocation = false; # Enable arena allocation for better performance
# Additional include paths
includePaths = [
"/usr/local/include"
"./third_party"
];
# gRPC support
grpc = {
enable = true;
generateMockCode = true; # Generate mock code for testing
options = [
"generate_mock_code=true"
];
};
# Embedded C/C++ support
nanopb = {
enable = true;
options = [
"max_size=1024"
"max_count=16"
];
};
# Pure C support
protobuf-c = {
enable = false;
options = [];
};
};

Bufrnix supports all modern C++ standards:

  • C++17: Default, widely supported
  • C++20: Modern features like concepts and coroutines
  • C++23: Latest standard with additional features
ModeDescriptionUse Case
SPEEDOptimize for execution speed (default)Server applications
CODE_SIZEOptimize for smaller binary sizeMobile/embedded devices
LITE_RUNTIMEUse lite runtime (smaller binary, fewer features)Resource-constrained

When cmakeIntegration is enabled, Bufrnix generates CMake configuration files:

# FindProtobuf.cmake generated content
find_package(Protobuf REQUIRED)
find_package(gRPC REQUIRED)
# Include generated proto headers
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/gen/cpp)
# Link against protobuf libraries
target_link_libraries(myapp
protobuf::libprotobuf
gRPC::grpc++
)

When pkgConfigIntegration is enabled, .pc files are generated for easy library discovery.

Enable gRPC to generate service stubs and client code:

grpc = {
enable = true;
generateMockCode = true; # Generate Google Mock compatible mocks
};

Generated files include:

  • Service interfaces (*.grpc.pb.h)
  • Client and server implementations (*.grpc.pb.cc)
  • Mock classes for testing (when enabled)

For embedded systems with limited RAM:

nanopb = {
enable = true;
options = [
"max_size=1024" # Maximum message size
"max_count=16" # Maximum repeated field count
"long_names=false" # Use shorter field names
];
};

For pure C environments:

protobuf-c = {
enable = true;
options = [];
};
flake.nix
{
languages.cpp = {
enable = true;
outputPath = "src/proto";
standard = "c++17";
};
}
main.cpp
#include "example.pb.h"
int main() {
example::v1::User user;
user.set_id(123);
user.set_name("Alice");
std::string serialized;
user.SerializeToString(&serialized);
return 0;
}
  1. Choose the Right Runtime: Use lite runtime for mobile/embedded, full runtime for servers
  2. Optimize Appropriately: Use SPEED for servers, CODE_SIZE for embedded
  3. Enable Arena Allocation: For high-performance scenarios with many small allocations
  4. Use Modern C++: Prefer C++17 or newer for better language features
  5. CMake Integration: Enable for easier build system integration
  1. Protobuf Version Mismatch

    # Ensure consistent versions
    protobufVersion = "3.27"; # Match your system protobuf
  2. Missing Headers

    # Add include paths
    includePaths = [
    "${pkgs.protobuf}/include"
    "./third_party/protobuf"
    ];
  3. gRPC Compilation Errors

    # Ensure gRPC is enabled with matching protobuf version
    grpc.enable = true;
    protobufVersion = "3.27"; # Must match gRPC's protobuf dependency