> ## Documentation Index
> Fetch the complete documentation index at: https://docs.reasonos.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C / C++ SDK

> Build C and C++ applications with RBS — compile, link, shared libraries, and hermetic launcher scripts.

# 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).

```python theme={null}
load("@rbs//c/rules.rbs", "c_library")

c_library(
    name = "math_lib",
    srcs = ["math.c"],
    hdrs = ["math.h"],
    cflags = ["-O2", "-Wall"],
)
```

| Attribute | Type   | Description                                       |
| --------- | ------ | ------------------------------------------------- |
| `name`    | string | Unique target name (required).                    |
| `srcs`    | list   | C source files (`.c`).                            |
| `hdrs`    | list   | Header files (`.h`).                              |
| `deps`    | list   | Dependencies (`c_library` or `c_shared_library`). |
| `cflags`  | list   | Extra compiler flags (e.g., `-O2`, `-Wall`).      |

#### `c_shared_library`

Creates a C shared library (`.so` / `.dylib`).

```python theme={null}
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.

```python theme={null}
load("@rbs//c/rules.rbs", "c_binary")

c_binary(
    name = "calculator",
    srcs = ["main.c"],
    deps = [":math_lib"],
    cflags = ["-O2"],
    ldflags = ["-lm"],
)
```

| Attribute | Type   | Description                    |
| --------- | ------ | ------------------------------ |
| `name`    | string | Unique target name (required). |
| `srcs`    | list   | C source files.                |
| `hdrs`    | list   | Header files.                  |
| `deps`    | list   | Dependencies.                  |
| `cflags`  | list   | Compiler flags.                |
| `ldflags` | list   | Linker flags.                  |

### C++ Rules

#### `cxx_library`

Creates a C++ static library.

```python theme={null}
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"],
)
```

| Attribute  | Type   | Description                       |
| ---------- | ------ | --------------------------------- |
| `name`     | string | Unique target name (required).    |
| `srcs`     | list   | C++ source files (`.cpp`, `.cc`). |
| `hdrs`     | list   | Header files (`.h`, `.hpp`).      |
| `deps`     | list   | Dependencies.                     |
| `cxxflags` | list   | C++ compiler flags.               |

#### `cxx_shared_library`

Creates a C++ shared library.

```python theme={null}
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.

```python theme={null}
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

```python filename="BUILD.rbs" theme={null}
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"],
)
```

```bash theme={null}
rbs build app        # Compile and link
rbs run app          # Run with correct library paths
```
