1

I am trying to write a few tests for a Rust - Java cross-platform program where Rust serves as a library for the backend. I want to call the tests from a new main.rs.

While the library (lib.rs) works fine when called from the Java code, I have trouble linking the main.rs to the lib.rs.

This works but has errors when cross-compiling:

Cargo.toml:

[package]
name = "rust_backend"
version = "0.1.0"
edition = "2018"
crate-type = ["cdylib"]

[dependencies]    
jni = { version = "0.10.2", default-features = false }

[profile.release]
lto = true

lib.rs

pub const ĆALLING: &'static str = "Rust library called";

pub fn foo() -> bool {
     return true;
}

main.rs

use rust_backend::CALLING;
use rust_backend::foo;

fn main() {
    println!("{}", CALLING);
    if foo() == true {    {
        println!("foo");
    }
}

I want my Cargo.toml to look like this so that cross-compiling works:

[package]
name = "rust_backend"
version = "0.1.0"
edition = "2018"

[dependencies]
jni = { version = "0.10.2", default-features = false }

[profile.release]
lto = true

[lib]
name = "rust_backend"
crate-type = ["cdylib"]

This results in an error when building:

error[E0432]: unresolved import `rust_backend`
 --> src/main.rs:1:5
  |
1 | use rust_backend::CALLING;
  |     ^^^^^^^^^^^^ use of undeclared type or module `rust_backend`

error[E0432]: unresolved import `rust_backend`
 --> src/main.rs:2:5
  |
2 | use rust_backend::foo;
  |     ^^^^^^^^^^^^ use of undeclared type or module `rust_backend`

I'm probably missing something super-simple here, but neither the documentation nor other code examples where able to help me out with this basic problem.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
madde
  • 47
  • 1
  • 8
  • It looks like your question might be answered by the answers of [Rust can't find crate](https://stackoverflow.com/a/49762980/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Dec 16 '19 at 21:15
  • 1
    TL;DR the duplicate: move your common code to a regular library then create a cdylib that uses the regular library. Put the tests in the regular library. – Shepmaster Dec 16 '19 at 21:16

0 Answers0