130 stable releases

1.0.129 Oct 15, 2024
1.0.124 Jun 14, 2024
1.0.120 Mar 23, 2024
1.0.111 Dec 17, 2023
0.3.4 May 22, 2020

#72 in Build Utils

Download history 239305/week @ 2024-07-18 218072/week @ 2024-07-25 189336/week @ 2024-08-01 213354/week @ 2024-08-08 196693/week @ 2024-08-15 197888/week @ 2024-08-22 196142/week @ 2024-08-29 210500/week @ 2024-09-05 202794/week @ 2024-09-12 197685/week @ 2024-09-19 216668/week @ 2024-09-26 231896/week @ 2024-10-03 242209/week @ 2024-10-10 250689/week @ 2024-10-17 243418/week @ 2024-10-24 226274/week @ 2024-10-31

1,009,852 downloads per month
Used in 329 crates (131 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

~1.7–9.5MB
~82K SLoC