Skip to main content

C / C++ SDK

RBS provides native C and C++ support with compilation, linking, shared library management, and hermetic launcher scripts that handle LD_LIBRARY_PATH / DYLD_LIBRARY_PATH automatically.

Rules

C Rules

c_library

Creates a C static library (object archive).
load("@rbs//c/rules.rbs", "c_library")

c_library(
    name = "math_lib",
    srcs = ["math.c"],
    hdrs = ["math.h"],
    cflags = ["-O2", "-Wall"],
)
AttributeTypeDescription
namestringUnique target name (required).
srcslistC source files (.c).
hdrslistHeader files (.h).
depslistDependencies (c_library or c_shared_library).
cflagslistExtra compiler flags (e.g., -O2, -Wall).

c_shared_library

Creates a C shared library (.so / .dylib).
load("@rbs//c/rules.rbs", "c_shared_library")

c_shared_library(
    name = "math_shared",
    srcs = ["math.c"],
    hdrs = ["math.h"],
    cflags = ["-O2"],
)

c_binary

Creates an executable C binary with a hermetic launcher script.
load("@rbs//c/rules.rbs", "c_binary")

c_binary(
    name = "calculator",
    srcs = ["main.c"],
    deps = [":math_lib"],
    cflags = ["-O2"],
    ldflags = ["-lm"],
)
AttributeTypeDescription
namestringUnique target name (required).
srcslistC source files.
hdrslistHeader files.
depslistDependencies.
cflagslistCompiler flags.
ldflagslistLinker flags.

C++ Rules

cxx_library

Creates a C++ static library.
load("@rbs//cxx/rules.rbs", "cxx_library")

cxx_library(
    name = "string_utils",
    srcs = ["string_utils.cpp"],
    hdrs = ["string_utils.hpp"],
    cxxflags = ["-std=c++17", "-O2"],
)
AttributeTypeDescription
namestringUnique target name (required).
srcslistC++ source files (.cpp, .cc).
hdrslistHeader files (.h, .hpp).
depslistDependencies.
cxxflagslistC++ compiler flags.

cxx_shared_library

Creates a C++ shared library.
load("@rbs//cxx/rules.rbs", "cxx_shared_library")

cxx_shared_library(
    name = "engine",
    srcs = ["engine.cpp"],
    hdrs = ["engine.hpp"],
    cxxflags = ["-std=c++17", "-O2"],
)

cxx_binary

Creates an executable C++ binary.
load("@rbs//cxx/rules.rbs", "cxx_binary")

cxx_binary(
    name = "app",
    srcs = ["main.cpp"],
    deps = [":string_utils", ":engine"],
    cxxflags = ["-std=c++17", "-O2"],
    ldflags = ["-lpthread"],
)

Shared Library Handling

When a c_binary or cxx_binary depends on shared libraries, RBS automatically:
  1. Copies shared libraries into the target’s runfiles/lib/ directory.
  2. Generates a launcher script that sets LD_LIBRARY_PATH (Linux) and DYLD_LIBRARY_PATH (macOS) to point to the bundled libraries.
This ensures the binary always finds its shared libraries without requiring system-wide installation.

Example

load("@rbs//c/rules.rbs", "c_binary", "c_library", "c_shared_library")

# Static library
c_library(
    name = "math_lib",
    srcs = ["src/math.c"],
    hdrs = ["include/math.h"],
    cflags = ["-O2", "-Wall", "-I", "include"],
)

# Shared library
c_shared_library(
    name = "io_lib",
    srcs = ["src/io.c"],
    hdrs = ["include/io.h"],
    cflags = ["-O2", "-I", "include"],
)

# Binary linking both
c_binary(
    name = "app",
    srcs = ["src/main.c"],
    deps = [":math_lib", ":io_lib"],
    cflags = ["-O2", "-I", "include"],
)
rbs build app        # Compile and link
rbs run app          # Run with correct library paths