Binding C Lib to Rust

I would like bind C library to Rust.

undefined reference to lib's functions

Project tree:

    ├── Cargo.toml
    ├── README
    └── src


    name = "my_lib"
    version = "0.1.0"
    authors = ["JPE"]
    edition = "2018"
    build = ""

    [lib] # Bindings 
    name = "my_lib"
    path = "src/"

    [[bin]]  # Bin with Examples 
    name = "my_lib_rust_example"
    path = "src/"

    libc = "0.2.71"

use std::env;

fn main()
    let dst = env::var("PWD").unwrap();

    println!("cargo:rustc-link-search=native={}/../", dst);
    println!("cargo:include={}/../../include", dst);

    println!("cargo:rustc-flags=-l dylib=stdc++");      //link c++ std library when using gcc

What is the library you are linking to? Is it just libstdc++ or is there another one?

Can you paste the exact linker error?

Link to log:

Hmm. I see that Cargo is passing -l dylib=stdc++ to rustc when building your library ( but not when building your binary ( I'm not sure why.

Does the same thing happen if you change the last line to print "cargo:rustc-link-lib=dylib=stdc++" instead?

the same effect.

I can reproduce this problem in a simple test case. I couldn't find any explanation for this behavior, or any existing bug reports about it, so I filed one here:

...and just after submitting that, now I found a duplicate issue. Oops!

From the previous issue:

This is expected and intended, otherwise you would get duplicate symbols. To work with this, the code using the libraries needs to be in, and should just use arrow_client.


Ok. I'm solved it. Thx.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.