143 stable releases

new 1.0.142 Feb 28, 2025
1.0.137 Jan 19, 2025
1.0.136 Dec 25, 2024
1.0.131 Nov 22, 2024
0.3.4 May 22, 2020

#35 in Build Utils

Download history 228445/week @ 2024-11-01 238026/week @ 2024-11-08 243461/week @ 2024-11-15 197615/week @ 2024-11-22 212248/week @ 2024-11-29 238783/week @ 2024-12-06 239717/week @ 2024-12-13 123088/week @ 2024-12-20 96231/week @ 2024-12-27 188240/week @ 2025-01-03 230117/week @ 2025-01-10 209313/week @ 2025-01-17 200661/week @ 2025-01-24 213500/week @ 2025-01-31 314619/week @ 2025-02-07 142764/week @ 2025-02-14

906,698 downloads per month
Used in 383 crates (148 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.8–9MB
~77K SLoC