Crate cpp [] [src]

This crate cpp only provides a single macro, the cpp! macro. This macro by itself is not useful, but when combined with the cpp_build crate it allows embedding arbitrary C++ code.

There are two variants of the cpp! macro. The first variant is used for raw text inclusion. Text is included into the generated C++ file in the order which they were defined, inlining module declarations.

cpp! {{
    #include <stdint.h>
    #include <stdio.h>

The second variant is used to embed C++ code within rust code. A list of variable names which should be captured are taken as the first argument, with their corresponding C++ type. The body is compiled as a C++ function.

This variant of the macro may only be invoked in expression context, and requires an unsafe block, as it is performing FFI.

let y: i32 = 10;
let mut z: i32 = 20;
let x: i32 = cpp!([y as "int32_t", mut z as "int32_t"] -> i32 as "int32_t" {
    return y + z;


This crate must be used in tandem with the cpp_build crate. A basic Cargo project which uses these projects would have a structure like the following:

|-- Cargo.toml
|-- src

Where the files look like the following:


build = ""

cpp = "0.3"

cpp_build = "0.3"

extern crate cpp_build;

fn main() {

extern crate cpp;

    #include <stdio.h>

fn main() {
    unsafe {
        cpp!([] {
            printf("Hello, World!\n");



This macro is used to embed arbitrary C++ code. See the module level documentation for more details.