#build-dependencies

build cxx-build

C++ code generator for integrating cxx crate into a Cargo build

149 releases (stable)

1.0.117 Feb 19, 2024
1.0.115 Jan 6, 2024
1.0.111 Dec 17, 2023
1.0.110 Oct 28, 2023
0.3.4 May 22, 2020

#5 in Build Utils

Download history 206213/week @ 2023-11-07 200410/week @ 2023-11-14 171078/week @ 2023-11-21 207816/week @ 2023-11-28 206034/week @ 2023-12-05 180362/week @ 2023-12-12 146955/week @ 2023-12-19 96787/week @ 2023-12-26 168031/week @ 2024-01-02 190409/week @ 2024-01-09 209676/week @ 2024-01-16 208114/week @ 2024-01-23 213999/week @ 2024-01-30 203105/week @ 2024-02-06 213941/week @ 2024-02-13 216503/week @ 2024-02-20

882,376 downloads per month
Used in 254 crates (108 directly)

MIT/Apache

375KB
10K SLoC

The CXX code generator for constructing and compiling C++ code.

This is intended to be used from Cargo build scripts to execute CXX's C++ code generator, set up any additional compiler flags depending on the use case, and make the C++ compiler invocation.


Example

Example of a canonical Cargo build script that builds a CXX bridge:

// build.rs

fn main() {
    cxx_build::bridge("src/main.rs")
        .file("src/demo.cc")
        .flag_if_supported("-std=c++11")
        .compile("cxxbridge-demo");

    println!("cargo:rerun-if-changed=src/main.rs");
    println!("cargo:rerun-if-changed=src/demo.cc");
    println!("cargo:rerun-if-changed=include/demo.h");
}

A runnable working setup with this build script is shown in the demo directory of https://github.com/dtolnay/cxx.


Alternatives

For use in non-Cargo builds like Bazel or Buck, CXX provides an alternate way of invoking the C++ code generator as a standalone command line tool. The tool is packaged as the cxxbridge-cmd crate.

$ cargo install cxxbridge-cmd  # or build it from the repo

$ cxxbridge src/main.rs --header > path/to/mybridge.h
$ cxxbridge src/main.rs > path/to/mybridge.cc

Dependencies

~0.7–1.4MB
~27K SLoC