# Testing \*-sys crates

**URL:** <https://users.rust-lang.org/t/testing-sys-crates/51519>\
**Category:** help\
**Created:** [November 15, 2020, 8:36pm UTC](https://users.rust-lang.org/t/testing-sys-crates/51519 "2020-11-15T20:36:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![darley](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/darley/32/18369_2.png) [@darley](https://users.rust-lang.org/u/darley)\
**Post date:** [November 15, 2020, 8:36pm UTC](https://users.rust-lang.org/t/testing-sys-crates/51519/1 "2020-11-15T20:36:21Z")

</div>

Hi, I am trying to run some tests in a crate that uses a custom \*-sys. Here's how my folder tree is organized:

```rust
tcl_tk-rs/
    - src/
    - Cargo.toml
    - tests/
    - tcl_tk-sys/
        - src/
        - build.rs
        - Cargo.toml

```

My `tcl_tk-rs/Cargo.toml` is

```nohighlight
[package]
name = "tk-rs"
version = "0.1.0"
authors = ["Darley Barreto"]
edition = "2018"

[lib]
name = "tcl_tk"
path = "src/lib.rs"

[dependencies]
num-bigint = "0.3"

[dependencies.tcl_tk-sys]
path = "tcl_tk-sys"
version = "0.1.0"

```

My `tcl_tk-rs/tcl_tk-sys/Cargo.toml` is

```nohighlight
[package]
name = "tcl_tk-sys"
version = "0.1.0"
authors = ["Darley Barreto"]
edition = "2018"
links = "tcl8.6"
build = "build.rs"

[dependencies]
libc = "*"

[build-dependencies]
pkg-config = "0.3.19"

[lib]
name = "tcl_tk_sys"
path = "src/lib.rs"

```

And my `build.rs` is

```rust
use pkg_config;

fn main() {
    if !build_pkgconfig() {
        println!("cargo:rustc-flags=-l tcl8.6");
        println!("cargo:rustc-flags=-l tk8.6");
    }
}

fn build_pkgconfig() -> bool {
    let conf = pkg_config::Config::new();

    if (&conf.probe("tcl8.6")).is_err() || (&conf.probe("tk8.6")).is_err() {
        print!("Could not find Tcl or Tk via pkgconfig");
        false
    } else {
        true
    }
}

```

Building is fine, but when I do `cargo test` lots of `cc` errors (`undefined reference to` related to symbols) show up. What am I doing wrong?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![MoAlyousef](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/moalyousef/32/17732_2.png) [@MoAlyousef](https://users.rust-lang.org/u/MoAlyousef)\
**Post date:** [November 15, 2020, 10:55pm UTC](https://users.rust-lang.org/t/testing-sys-crates/51519/2 "2020-11-15T22:55:00Z")

</div>

Your build script could be failing. You can’t rely on print! or println! macros. It’s better to panic with a message. Also regarding rustc-flags, I’m not sure these are passed to the linker. Why not use rustc-link-lib?  
I also noticed a lib name discrepancy "tcl\_tk\_sys", it could just be a typo. It’s also unusual to see "so numbers" passed directly outside the context of dl-loading.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kornel/32/4823_2.png) [@kornel](https://users.rust-lang.org/u/kornel)\
**Post date:** [November 16, 2020, 10:21am UTC](https://users.rust-lang.org/t/testing-sys-crates/51519/3 "2020-11-16T10:21:58Z")

</div>

I've mainly seen `rustc-link-lib` used for linking instead of `rustc-flags=-l`. I'm not sure if there's a material difference between these two.

When developing build scripts, build with `cargo build -vvv`. Otherwise Cargo "eats" script's output and you won't see what is happening.

You can also use:

```rust
println!("cargo:warning=Could not find Tcl or Tk via pkgconfig")

```

to make the text visible to users _on failure_. However, failure happens only if your build script returns failing exit code (e.g. panics). If you just set the flags and hope for the best, it will fail at link time with a _horrible_ unreadable huge dump of hundreds of command-line flags. It's better to complain loudly early.

---

<div class="post-metadata">

**Author:** ![darley](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/darley/32/18369_2.png) [@darley](https://users.rust-lang.org/u/darley)\
**Post date:** [November 16, 2020, 11:05am UTC](https://users.rust-lang.org/t/testing-sys-crates/51519/4 "2020-11-16T11:05:21Z")

</div>

Hi, I'm really new to Rust, so there are some things I don't know, for instance:

> Why not use rustc-link-lib

What would this be?

> I also noticed a lib name discrepancy "tcl\_tk\_sys", it could just be a typo

If you mean this in the `tcl_tk-rs/tcl_tk-sys/Cargo.toml`:

```toml
[lib]
name = "tcl_tk_sys"
path = "src/lib.rs"

```

It wasnt, I wrote that so I can use

```rust
use tcl_tk_sys::*;

```

> It’s also unusual to see "so numbers" passed directly outside the context of dl-loading

You mean `links = "tcl8.6"` in the Cargo.toml?

---

<div class="post-metadata">

**Author:** ![darley](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/darley/32/18369_2.png) [@darley](https://users.rust-lang.org/u/darley)\
**Post date:** [November 16, 2020, 11:09am UTC](https://users.rust-lang.org/t/testing-sys-crates/51519/5 "2020-11-16T11:09:58Z")

</div>

> println!("cargo:warning=Could not find Tcl or Tk via pkgconfig")

It does say `warning: Could not find Tcl or Tk via pkgconfig`, but nothing else. And I've changed to

```rust
fn main() {
    if !build_pkgconfig() {
        println!("cargo:rustc-link-lib=dylib=tcl8.6");
        println!("cargo:rustc-link-lib=dylib=tk8.6");
    }
}

```

---

<div class="post-metadata">

**Author:** ![kornel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kornel/32/4823_2.png) [@kornel](https://users.rust-lang.org/u/kornel)\
**Post date:** [November 16, 2020, 12:22pm UTC](https://users.rust-lang.org/t/testing-sys-crates/51519/6 "2020-11-16T12:22:29Z")

</div>

Cargo prints warning lines even without `-vvv` **if** your script panics. Your script doesn't panic, and falls back to flags that will fail at link time, which is too late to correlate that failure with your script, so user will not get the warning.

I suggest: don't print the link directives unless you've _proven_ that they will work. Check if the files exist, or require an explicit opt-in, e.g. env var for `TCL_LIB_DIR` that you set as the search path first.

Don't set up linking for failure, because that's an error you can't handle in your script, and that error is handled very very poorly by Rust (it basically does nothing about it, and lets system linker just tell "f\*\*\* you" to the user).

---

<div class="post-metadata">

**Author:** ![darley](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/darley/32/18369_2.png) [@darley](https://users.rust-lang.org/u/darley)\
**Post date:** [November 16, 2020, 12:42pm UTC](https://users.rust-lang.org/t/testing-sys-crates/51519/7 "2020-11-16T12:42:25Z")

</div>

Hmm, I see. Another option that failed was:

```rust
use pkg_config;

fn main() {
    let mut config = pkg_config::Config::new();
    let epec = config.exactly_version("8.6");

    if epec.probe("tcl").is_ok(){
        panic!("Could not find tcl8.6 using pkg-config");
    }

    if epec.probe("tk").is_ok(){
        panic!("Could not find tk8.6 using pkg-config");
    }
}

```

But then this worked for me

```rust
fn main() {
    println!("cargo:rustc-link-lib=dylib=tcl8.6");
    println!("cargo:rustc-link-lib=dylib=tk8.6");
}

```

What would you recommend to do?

---

<div class="post-metadata">

**Author:** ![kornel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kornel/32/4823_2.png) [@kornel](https://users.rust-lang.org/u/kornel)\
**Post date:** [November 16, 2020, 1:39pm UTC](https://users.rust-lang.org/t/testing-sys-crates/51519/8 "2020-11-16T13:39:07Z")

</div>

In your code `is_ok()` makes the condition backwards. Also `epec.probe("tk").expect("Could not find tcl8.6 using pkg-config")` does the same thing, but shorter.

> [@darley](#):
>
> But then this worked for me
> 
> ```rust
> fn main() {
> println!("cargo:rustc-link-lib=dylib=tcl8.6");
> 
> ```

It is possible that you have the library, but don't have `.pc` files that describe where it's installed. That's tough, because just setting the linker flag doesn't give you any chance to handle the case when the library is missing.

I suggest:

```rust
if pkg_config_didnt_work {
   if let Ok(dir) = std::env::var("TCL_LIB_DIR") {
      println!("cargo:rustc-link-search={}", dir);
      println!("cargo:rustc-link-lib=dylib=tcl8.6");
   } else {
      panic!("Can't find tcl via pkg-config. Set TCL_LIB_DIR env var for manual dir");
   }
}

```

---

<div class="post-metadata">

**Author:** ![darley](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/darley/32/18369_2.png) [@darley](https://users.rust-lang.org/u/darley)\
**Post date:** [November 16, 2020, 1:41pm UTC](https://users.rust-lang.org/t/testing-sys-crates/51519/9 "2020-11-16T13:41:07Z")

</div>

Thank you so much for this 😃

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [February 14, 2021, 1:41pm UTC](https://users.rust-lang.org/t/testing-sys-crates/51519/10 "2021-02-14T13:41:11Z")

</div>

Closed on February 14, 2021, 1:41pm UTC

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.
