154 releases (stable)

1.0.122 May 7, 2024
1.0.120 Mar 23, 2024
1.0.111 Dec 17, 2023
1.0.110 Oct 28, 2023
0.3.4 May 22, 2020

#42 in Build Utils

Download history 200610/week @ 2024-01-25 211047/week @ 2024-02-01 202505/week @ 2024-02-08 228990/week @ 2024-02-15 253041/week @ 2024-02-22 227019/week @ 2024-02-29 230175/week @ 2024-03-07 214456/week @ 2024-03-14 228085/week @ 2024-03-21 222483/week @ 2024-03-28 232281/week @ 2024-04-04 248576/week @ 2024-04-11 251865/week @ 2024-04-18 236097/week @ 2024-04-25 233036/week @ 2024-05-02 196961/week @ 2024-05-09

964,563 downloads per month
Used in 278 crates (120 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")
        .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–8.5MB
~68K SLoC