cxx icon indicating copy to clipboard operation
cxx copied to clipboard

Calling a C++ library (that includes another library)

Open fgadaleta opened this issue 3 years ago • 1 comments

I hope I am not posting in the wrong place. Is is possible to add an example in which one wants to call a C++ class method that, in turn, does some operations from another library (part of the C++ project). How would the build.rs compile such a complex C++ library? (Provided that is even possible).

My attempt is to let C++ devs feel at home and never cross the bridge.

fgadaleta avatar Aug 21 '22 15:08 fgadaleta

You can link the external library .

build.rs

fn main() {
  ...
  ...
    println!("cargo:rustc-link-lib=LIB");
    println!("cargo:rustc-link-search=LIB_PATH");
}

example

fn main() {
  ...
  ...
    println!("cargo:rustc-link-lib=nghttp2");
    println!("cargo:rustc-link-search=/usr/lib/");
}

younghyunjo avatar Aug 22 '22 10:08 younghyunjo

I think build.rs compilation of complex C++ library is tangential to cxx. You'd do that part exactly the same as you would without cxx involved, such as using the cc crate or cmake crate, or a polyglot build system like Bazel.

dtolnay avatar Nov 08 '22 10:11 dtolnay