Multiple files, multiple binaries and a library?

Hello,

I have a project with the following structure.

├── src
│ ├── algorithms
│ │ ├── file1.rs
│ │ ├── file2.rs
│ ├── bin
│ │ ├── main1.rs
│ │ ├── main2.rs
│ ├── lib.rs

On lib.rs:
pub mod algorithms {
pub mod file1;
pub mod file2;
}

On main1.rs:
use algorithms::file1::fnc1;

Cargo.toml:
[[bin]]
name = "main1"
path = "src/bin/main1.rs"

[[bin]]
name = "main2"
path = "src/bin/main2.rs"

Now, I want to create a library from that. So I added the following to my Cargo.toml:

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

And now the "use" on main files cannot find the internal files.

How can I solve it? Te goal is to allow something like it to work:

cargo build --bin main1
cargo build --lib

the cdylib crate type cannot be used as rust extern crate, use "lib" instead.

if for some reason, the library must be built as cdylib, then the binary crate must import it via ffi as if it were a C library, and only use functions exported with the #[unsafe(no_mangle)] attribute.

Thank you!!

But I need to generate a dll. Can I do that without creating a new project (and without changing what is working)?

you need to provide more details. what is this dll used for?

you can specify multiple crate types for the library crate, e.g.:

[lib]
name = "mylib"
crate-type = ["lib", "cdylib"]

note, this probably will make the code compile, but it may or may not work as you intended, it depends on the exact use case of the dll, so you need to provide more information.

the cdylib crate will have rust standard libraries all linked, and can be linked/loaded by programs in other languages, but it cannot be imported as regular rust extern crate, particularly, it is NOT linked as the dependency of the bin crate.

on the other hand,. the lib crate doesn't include the standard library, CANNOT be linked by other languages, but it is required to be imported by the bin crates.

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.