Skip to content

Questions about using Cargo to build a Rust library used from C++ #291

Closed
@Timmmm

Description

@Timmmm

I'm trying to set up a simple Rust library with a C++ interface, based on the demo in this repo, using Cargo. I have quite a few questions, hope that's ok:

  1. In build.rs it passes src/demo.cc to the code generator/compiler. If I'm only exposing Rust code to C++, and not the other way around, presumably I only need this if I have custom C++ bridging code, e.g. friendlier wrappers of the shared structs?

  2. If I remove these two lines:

     .file("src/demo.cc")
     .flag_if_supported("-std=c++14")
    

then I get C++11 errors, in other words C++11 is always required. But doesn't that mean that the compiler should add -std=c++11 even if you don't explicitly pass that flag? Also what happens if you're using MSVC? I would have expected an API like .cpp_standard(CppStandard::cxx14).

  1. I got a basic program working. After many link errors I figured out I have to link with the Rust dynamic library (libdemo.dylib) and with the cxxbridge static library (libcxxbridge-demo.a) and the cxx library (libcxxbridge04.a). Can you explain what all those are?

  2. libcxxbridge-demo.a and libcxxbridge04.a don't seem to get copied to a canonical location. In other words I have to link with target/debug/build/demo-78993d207bfe982b/out/libcxxbridge-demo.a and target/debug/build/cxx-f3cb2e64024d3552/out/libcxxbridge04.a. That doesn't seem right? I'd have expected them all to be copied or moved to target/debug/.

  3. If I have a function like this: fn get_cpp_string() -> CxxString; it says returning C++ string by value is not supported. Is that a permanent thing? I'd really like the exposed C++ API to use standard C++ types rather than cxx types because it is a public API rather than something internal. I can't see any reason why you couldn't return a std::string by value.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions