[package]
name = "foo"
version = "0.1.0"
authors = ["Hasan Yousef"]
[lib]
name = "foo"
crate-type = ["dylib", "rlib"]
exactname = {dylib = "{}.so", rlib = "{}.rlib"}
[dependencies]
and by running cargo build I got the libfoo.dylib and libfoo.rlib files generated in the target folder.
I copied the 2 generated files above to a lib folder in another project, which have the below 2 files:
main.rs:
extern crate libfoo;
use libfoo::*;
fn main() {
println!("Hello, my world!");
public_function();
}
Cargo.toml:
[package]
name = "app"
version = "0.1.0"
authors = ["Hasan Yousef"]
[dependencies]
libfoo = { path = "./libs" }
Once running the cargo run I got the below error:
error: no matching package named libfoo found (required by app)
location searched: file:///Users/hasan/Documents/RUST/rustmac/app/libs
version required: *
I'm not sure what you mean by "portable lib," but Rust dylibs are not at all portable. Maybe you're looking for cdylib? Then you probably also want to use extern "C" on your public functions.
The [dependencies] section in Cargo.toml is meant for source dependencies. With path = ... you are specifying a path to the source code of the crate you want to depend on. You can't use it for binary dependencies. If you want to link to a cdylib, check out FFI - The Rust Programming Language
Note that all Cargo.toml dependencies must be Rust source packages. There is no way to include any .so/.dylib/.a/.rlib directly as a Cargo dependency.
If you're just trying to use Rust code from another Rust project, then the dylib/cdylib path is most likely inappropriate and unnecessary complication. For linking Rust code you should "link" Cargo projects to Cargo projects.
If you're trying to explicitly make a C-compatible shared library linked at run time, then you have to either write linker commands in a custom build.rs script, or add #[link(…)] extern "C" {} to your code.