3

I'm building an app that hooks some Windows events. The hook code must be in a DLL for that to work. I set [lib] with crate-type = ["cdylib"] in Cargo.toml and use it in main.rs.

│   Cargo.toml
│
└───src
        lib.rs
        main.rs

It works.

I want to add a second binary, so use this structure:

└───src
    │   lib.rs
    │
    └───bin
            bin1.rs
            bin2.rs

It works.

bin1.rs and bin2.rs have some duplicated code that I want to moved to a new app.rs module, so I try this:

└───src
    │   lib.rs
    │
    └───bin
            app.rs
            bin1.rs
            bin2.rs

This causes an error 'main' function not found in crate 'app'. That is because all files in bin are expected to be built as binaries.

I move app.rs out of bin and try a different structure:

└───src
    │   lib.rs
    │   app.rs
    │
    └───bin
            bin1.rs
            bin2.rs

With that structure, bin1.rs and bin2.rs can't access app.rs.

Usually the shared code would go in lib.rs, but that is my DLL.

How should this situation be handled?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Adamarla
  • 739
  • 9
  • 20
  • You can be explicit in your `Cargo.toml` and not rely on convention – Boiethios Jan 28 '20 at 10:54
  • TL;DR the duplicates: Create a Rust library with the shared code. Use that shared code in your binaries, as well as in *another* crate that produces your `cdylib`. A workspace may come in nicely. – Shepmaster Jan 28 '20 at 15:48

0 Answers0