How to link with a dylib

Hi dear all,
I'm trying the following code

pub fn hello() {
  println!("hello Rust");

compile it with following command

rustc --crate-type rlib

got a libmylib.rlib, write a test

extern crate mylib;
fn main() {

comiple this file with following command line

rustc -L .

it works. then I deleted libmylib.rlib and compile to a dylib

rustc --crate-type dylib

got mylib.dll and mylib.dll.lib, this time the following command failed

rustc -L .

So how to link to a Rust dynlib ? Many thanks!!!

Try passing -C prefer-dynamic to rustc when compiling both crates. This will make both of them link the standard libraries dynamically, so that you don't end up with conflicting copies.

rustc -C prefer-dynamic --crate-type dylib
rustc  -C prefer-dynamic -L .    

You will also need to make sure the dynamic linker is able to locate the standard library DLLs when you run your program. (On Windows, I believe you can do this either by copying them to the program directory, or by adding their location to the PATH environment variable.)

1 Like

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.