Link attribute in library ignored when linking


#1

Hi,

I want to access WinRT API functions from a Rust program. When I create a simple executable with an extern block, the program compiles and links. As an example, I created this source file as main.rs in my project:

#[link(name="mincore", kind="static")]
extern "system" {
	fn RoUninitialize();
}

fn main() {
    unsafe {RoUninitialize();}
}

When I now create a library, e.g. define a lib.rs file with

pub mod roapi {

#[link(name="mincore", kind="static")]
extern "system" {
	fn RoUninitialize();
}

pub fn uninit() {
	unsafe {RoUninitialize();}
}
}

and change my executable to use the library

fn main() {
    use mycrate::roapi;

   roapi::uninit();
}

I get linker errors complaining about unresolved externals. I can fix this in the executable by adding an extern block here and then the library gets linked, but this feels wrong because it has already been defined in the lib.rs.

My Cargo.toml also has a buildscript which sets the path, and the package section has a

links = “mincore”

field.

Cheers,
Jens


#2

Don’t use kind="static", it is definitely the wrong thing and not what you want. mincore.lib is an import library to symbols from DLLs, so you really want kind="dylib". Also note that providing bindings to symbols like from those in mincore is part of winapi's reach, so you should be able to just use that in the future instead of writing your own raw bindings.


#3

Hi,

thanks for the hint with the dynamic library kind. However, it doesn’t solve my problem. I still get unresolved externals when I compile an example that does not have an extern block in the main source file.

Is there an example anywhere which shows how to achieve what I am trying to do?