dtolnay / cxx

Safe interop between Rust and C++
https://cxx.rs
Apache License 2.0
5.82k stars 330 forks source link

Export libcxxbridge1.a #1293

Open NoeMurr opened 9 months ago

NoeMurr commented 9 months ago

Hi all,

I need help to understand how to properly compile the bindings setting the target paths.

current situation

I'm building a rust library let's call it lib_a. For this library I created the bindings for c++ and they are compiled as the library cpp-bindings. To be able to compile a c++ library correctly I have to link also the library libcxxbridge1.a.

this is my build.rs file:

use std::env;

fn main() {
    let out_dir = env::var("CARGO_TARGET_DIR").unwrap_or("target".to_owned()) + "/" + &env::var("PROFILE").unwrap() + "/cpp-bindings";

    cxx_build::bridge("src/interface.rs")
        .flag_if_supported("-std=c++11")
        .out_dir(out_dir)
        .compile("cpp-bindings");

    println!("cargo:rerun-if-changed=src/interface.rs");
}

Like that, when I'm building in rust I get a filesystem's structure that is similar to:

project/
    |--> target
            |--> cxxbridge
                |--> bindings
                    |--> src
                        |--> interface.rs (link to interface.rs.h)
                        |--> interface.rs.h 
                |--> rust
                    |--> cxx.h (link to crate)
            |--> build
                 [...]
                 |--> cxx-<HASH> 
                    [...] 
                    |--> libcxxbridge1.a
            |--> <PROFILE>
                 [...]
                 |--> lib_a.so
                 |--> cpp-bindings
                     [...]
                     |--> libcpp-bindings.a

Desired situation

I would like to be able to have all the libraries that are needed for c++ in one single directory under the target/<PROFILE> directory. something like:

project
    |--> target
        |--> <PROFILE>
            [...]
            |--> cxxbridge
                |--> bindings-headers
                    [... identical to above ... ]
            |--> libcxxbridge1.a (could be a link)
            |--> libcpp-bindings.a (could be a link)
            |--> lib_a.so

The perfect solution would be to be able to also join together the library libcxxbridge1.a and libcpp-bindings.a but that's just a plus the important part would be to be able to access to them in an easy way.