#cpp #c #compiler

macro cpp_macros

Procedural macro implementation for the cpp crate

16 releases

0.5.9 Aug 16, 2023
0.5.8 Mar 30, 2023
0.5.7 Apr 29, 2022
0.5.6 Dec 29, 2020
0.2.1 Mar 22, 2017

#5 in #cxx

Download history 64476/week @ 2023-12-14 39883/week @ 2023-12-21 31688/week @ 2023-12-28 59518/week @ 2024-01-04 59139/week @ 2024-01-11 71638/week @ 2024-01-18 74802/week @ 2024-01-25 73479/week @ 2024-02-01 64400/week @ 2024-02-08 87966/week @ 2024-02-15 123202/week @ 2024-02-22 83044/week @ 2024-02-29 91874/week @ 2024-03-07 68536/week @ 2024-03-14 69732/week @ 2024-03-21 52061/week @ 2024-03-28

292,971 downloads per month
Used in 262 crates (via cpp)


753 lines

rust-cpp - Embed C++ code directly in Rust



rust-cpp is a build tool & macro which enables you to write C++ code inline in your rust code.

let name = std::ffi::CString::new("World").unwrap();
let name_ptr = name.as_ptr();
let r = unsafe {
    cpp!([name_ptr as "const char *"] -> u32 as "int32_t" {
        std::cout << "Hello, " << name_ptr << std::endl;
        return 42;
assert_eq!(r, 42)

The crate also help to expose some C++ class to Rust by automatically implementing trait such as Drop, Clone (if the C++ type can be copied), and others

    unsafe struct MyClass as "std::unique_ptr<MyClass>"


For usage information and in-depth documentation, see the cpp crate module level documentation.

Differences with the cxx crate

This crate allows to write C++ code "inline" within your Rust functions, while with the cxx crate, you have to write a bit of boiler plate to have calls to functions declared in a different .cpp file.

Having C++ code inline might be helpful when trying to call to a C++ library and that one may wish to make plenty of call to small snippets. It can otherwise be fastidious to write and maintain the boiler plate for many small functions in different places.

These crate can also be used in together. The cxx crate offer some useful types such as CxxString that can also be used with this crate.

The cxx bridge does more type checking which can avoid some classes of errors. While this crate can only check for equal size and alignment.


~31K SLoC