I would like to make a project that contains:
The directory structure, excluding temporary files and other trash:
.
├── Cargo.toml
├── src
│ ├── c_bindings.rs // contains C bindings for library
│ ├── compression.rs
│ ├── const_data.rs
│ ├── hash.rs
│ ├── lib.rs // library
│ └── main.rs // simple executable that uses library
└── target
└── debug
├── gost_stribog
├── libgost_stribog.rlib
I want cargo build
do this:
c_bindings.rs
c_bindings.rs
The debug directory should be:
└── target
└── debug
├── gost_stribog
├── libgost_stribog.rlib
├── libgost_stribog.so
What should my Cargo.toml
look like?
Opinionated answer: don't.
Instead, split your code into two or three separate crates:
Then, move your c_bindings.rs
to the bindings crate as just lib.rs
. It can depend on the core library. You can also move main.rs
into another crate that also depends on the core library.
These three crates can be in the same source code repository, but will be built with separate invocations.
A Cargo workspace may prove to be useful; in fact it's listed as an explicit reason ("An FFI crate with a sub-crate for FFI bindings").
Actually, you can. and here is how. $PROJECT_ROOT/.cargo/config.rs
[build]
rustflags = ["-C", "prefer-dynamic"]
remeber that you will need to distribute your application with the rust standard library as well.
$ ls $(rustc --print=sysroot)/lib
libLLVM-13-rust-1.57.0-beta.so librustc_driver-3aebdc12af579500.so libstd-c8bc39dac3997df6.so libtest-4d997b51b1a49b1f.so rustlib
in my case it's named libstd-c8bc39dac3997df6.so, because I'm on linux, if you are on windows, it should be in the bin folder instead of the lib folder.
also, you will need a script to run the program on linux and on MacOS. on linux and on macos you dont just put the library in the same directory as you application and expect it to work, instead you have 2 options.
/usr/lib
folder and then run it.exec LD_LIBRARY_PATH=$PWD ./YOU_APPLICATION $@ # this last commands tells sh to get the command line arguments.
Just that. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With