#build-dependencies #cpp #compile #cc #std #c-11 #cxxbridge-demo

build cxx-build

C++ code generator for integrating cxx crate into a Cargo build

159 stable releases

1.0.158 Apr 24, 2025
1.0.153 Mar 30, 2025
1.0.136 Dec 25, 2024
1.0.131 Nov 22, 2024
0.3.4 May 22, 2020

#24 in Build Utils

Download history 206032/week @ 2025-01-22 197988/week @ 2025-01-29 261370/week @ 2025-02-05 338797/week @ 2025-02-12 348705/week @ 2025-02-19 356148/week @ 2025-02-26 360411/week @ 2025-03-05 385778/week @ 2025-03-12 1063432/week @ 2025-03-19 670514/week @ 2025-03-26 551888/week @ 2025-04-02 510030/week @ 2025-04-09 362383/week @ 2025-04-16 383280/week @ 2025-04-23 349092/week @ 2025-04-30 357704/week @ 2025-05-07

1,551,135 downloads per month
Used in 416 crates (156 directly)

MIT/Apache

380KB
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

~2–9MB
~80K SLoC

OSZAR »