135 stable releases

new 1.0.134 Dec 11, 2024
1.0.131 Nov 22, 2024
1.0.124 Jun 14, 2024
1.0.120 Mar 23, 2024
0.3.4 May 22, 2020

#41 in Build Utils

Download history 203269/week @ 2024-08-25 201375/week @ 2024-09-01 207206/week @ 2024-09-08 188639/week @ 2024-09-15 211778/week @ 2024-09-22 227468/week @ 2024-09-29 234737/week @ 2024-10-06 246170/week @ 2024-10-13 248189/week @ 2024-10-20 230695/week @ 2024-10-27 235558/week @ 2024-11-03 246940/week @ 2024-11-10 228743/week @ 2024-11-17 184431/week @ 2024-11-24 233680/week @ 2024-12-01 228005/week @ 2024-12-08

893,797 downloads per month
Used in 348 crates (136 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–8.5MB
~77K SLoC