Project structure

Hello @all,

i'm new here and Rust is for me total new. I want to understand an programming with Rust.
Yeah and my first border is the project structure self.
That works for so far. :smiley:

I have two files:
1.) src/main.rs
2.) src/lib.rs.

I need src/main.rs as exe and src/lib.rs as dll. Both files are in the same project. I have read this tutorial but nothing happens. https://gist.github.com/CoolOppo/67b452c125bb0db3212a9fbc44c84245

What i want is basicly an executeable file and dll file from one project. The dll file is used by the executeable file.

Has anybody a tutorial for me how can i solve this?

Best thanks.

This tutorial gives wrong advice. You should never have mod lib in a Cargo project. It duplicates code and reuses lib.rs in a way that is not expected by Cargo.

When you have both src/main.rs and src/lib.rs, Cargo will use lib.rs to build a Rust library, and then statically link that library into the binary built from src/main.rs. The binary will not depend on a DLL. Even if you build DLL at the same time, it will not be used by the binary from the same crate.

Are you sure you want to use a DLL here? It takes much more effort, as you can't use Rust functions on the DLL boundary. You will need to build a stable C API for it. It's a lot of work, and Cargo doesn't support automatically linking with such DLL. That will need making three crates (cdylib crate, sys crate, and the binary crate), a manual build step, and a build script.

3 Likes

Thank for your response and the helpful link about sys crate.

Dont bully me when i say i have Windows as Operationsystem and want to programming with Rust. :smiley:

For me is a testing thing to understand how Rust is working now. I know C# and i have spent lot of time in this language.
You have in C# the possibility to create many Projects in one solution and when the project is a library then they will compile as DLL and all other Projects as executable file.

The only one what i need to understand, how can i work with libraries and what is the best practices.

Best thanks.

In Rust, your libraries are typically compiled into the executable rather than as a DLL.

Thanks...

I understand it now

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.