How to force dynamic linking to the examples code of a library crate?

As in the Build Scripts - The Cargo Book :

The -l flag is only passed to the library target of the package, unless there is no library target, in which case it is passed to all targets. This is done because all other targets have an implicit dependency on the library target, and the given library to link should only be included once. This means that if a package has both a library and a binary target, the library has access to the symbols from the given lib, and the binary should access them through the library target’s public API.

I have a library crate and in its build.rs, I link to a shared library by:

println!("cargo:rustc-link-lib=dylib=my_lib");

But the shared library is not linked for my examples code. I want the -l flag to be passed to my code under /examples folder as well. Is there a way to do that?

Thanks for the help in advance!

how did you get to that conclusion? in most common use cases, add the native library as dependency to the library target should be enough, as stated in the book:

all other targets have an implicit dependency on the library target

if you have uncommon usage scenarios, please describe in detail.

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.