Binding C Lib to Rust

Hello.
I would like bind C library to Rust.

errors:
undefined reference to lib's functions

https://pastebin.com/uLivDzKg

Project tree:

    ├── build.rs
    ├── Cargo.toml
    ├── README
    └── src
        ├── API.rs
        └── main.rs

Cargo.toml:

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

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

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

    [dependencies]
    libc = "0.2.71"

build.rs:


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: https://pastebin.com/uLivDzKg

Hmm. I see that Cargo is passing -l dylib=stdc++ to rustc when building your library (API.rs) but not when building your binary (main.rs). 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?

https://pastebin.com/9aD3ZGJX

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 lib.rs, and main.rs should just use arrow_client.

3 Likes

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.