153 releases (stable)

1.0.121 Apr 8, 2024
1.0.120 Mar 23, 2024
1.0.118 Feb 28, 2024
1.0.111 Dec 17, 2023
0.3.4 May 22, 2020

#44 in Build Utils

Download history 183335/week @ 2024-01-05 197668/week @ 2024-01-12 212621/week @ 2024-01-19 198924/week @ 2024-01-26 214043/week @ 2024-02-02 199934/week @ 2024-02-09 236702/week @ 2024-02-16 258172/week @ 2024-02-23 219436/week @ 2024-03-01 229361/week @ 2024-03-08 217600/week @ 2024-03-15 228400/week @ 2024-03-22 218489/week @ 2024-03-29 234734/week @ 2024-04-05 255187/week @ 2024-04-12 204052/week @ 2024-04-19

955,981 downloads per month
Used in 271 crates (115 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
~66K SLoC