Rust executable linking to a system library works, but a library has unresolved references

Viewed 888

I am trying to use WinRT function from Rust in a library. When I create a simple example program which declares external functions and uses them everything compiles and links:

extern crate winapi;
use winapi::winerror::{HRESULT};

#[repr(C)]
pub enum RO_INIT_TYPE {
    RO_INIT_SINGLETHREADED = 0,
    RO_INIT_MULTITHREADED  = 1      
}

#[link(name="mincore", kind="static")]
extern "system" {
    pub fn RoInitialize(threadingModel: RO_INIT_TYPE) -> HRESULT;
    pub fn RoUninitialize();
}

fn main() {
    unsafe {
        RoInitialize(RO_INIT_TYPE::RO_INIT_MULTITHREADED);
        RoUninitialize();
    }
}

However, when I try to create a library which encapsulates the WinRT API, I get unresolved references from the linker. I have a library winrt with a module roapi which contains

#[repr(C)]
pub enum RO_INIT_TYPE {
    RO_INIT_SINGLETHREADED = 0,
    RO_INIT_MULTITHREADED  = 1      
}

#[link(name="mincore", kind="static")]
extern "system" {
    pub fn RoInitialize(threadingModel: RO_INIT_TYPE) -> HRESULT;
    pub fn RoUninitialize();
}

pub struct RoApi {
    marker: PhantomData<()>
}

impl RoApi {
    pub fn multi_threaded() -> Result<RoApi, HRESULT>

    pub fn single_threaded() -> Result<RoApi, HRESULT>

I thought that the link attribute would specify that whenever the lib is linked, mincore is a dependency and is also linked. When I now create a sample program which uses this library, the mincore dependency is not used:

extern crate winrt;

fn main() {
    use winrt::RoApi;

    let api = RoApi::multi_threaded().unwrap();
}
note: "G:\\Programme\\Visual Studio 2015\\VC\\bin\\amd64\\link.exe" "/LIBPATH:G:\\Programme\\Visual Studio 2015\\VC\\lib\\amd64" "/LIBPATH:C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.10586.0\\ucrt\\x64" "/LIBPATH:C:\\Program Files (x86)\\Windows Kits\\10\\lib\\10.0.10586.0\\um\\x64" "/NOLOGO" "/NXCOMPAT" "/LIBPATH:C:\\Program Files\\Rust stable MSVC 1.6\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "G:\\dev\\rustrt\\target\\debug\\examples\\simpleExample.0.o" "/OUT:G:\\dev\\rustrt\\target\\debug\\examples\\simpleExample.exe" "/OPT:REF,ICF" "/DEBUG" "/LIBPATH:G:\\dev\\rustrt\\target\\debug" "/LIBPATH:G:\\dev\\rustrt\\target\\debug\\deps" "/LIBPATH:C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.10240.0\\um\\x86" "/LIBPATH:C:\\Program Files\\Rust stable MSVC 1.6\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "G:\\dev\\rustrt\\target\\debug\\libwinrt.rlib" "G:\\dev\\rustrt\\target\\debug\\deps\\liblibc-c862fb1c783dd674.rlib" "G:\\dev\\rustrt\\target\\debug\\deps\\libwinapi-96db160368c72f00.rlib" "C:\\Program Files\\Rust stable MSVC 1.6\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libstd-ca9f0d77.rlib" "C:\\Program Files\\Rust stable MSVC 1.6\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcollections-ca9f0d77.rlib" "C:\\Program Files\\Rust stable MSVC 1.6\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_unicode-ca9f0d77.rlib" "C:\\Program Files\\Rust stable MSVC 1.6\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librand-ca9f0d77.rlib" "C:\\Program Files\\Rust stable MSVC 1.6\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liballoc-ca9f0d77.rlib" "C:\\Program Files\\Rust stable MSVC 1.6\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liballoc_system-ca9f0d77.rlib" "C:\\Program Files\\Rust stable MSVC 1.6\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liblibc-ca9f0d77.rlib" "C:\\Program Files\\Rust stable MSVC 1.6\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcore-ca9f0d77.rlib" "msvcrt.lib" "ws2_32.lib" "userenv.lib" "shell32.lib" "advapi32.lib" "msvcrt.lib" "compiler-rt.lib"
note: libwinrt.rlib(winrt.0.o) : error LNK2019: unresolved external symbol RoInitialize referenced in function _ZN5roapi14initialize_api20h8bd5c81277d9b36ePaaE
libwinrt.rlib(winrt.0.o) : error LNK2019: unresolved external symbol RoUninitialize referenced in function _ZN5roapi13_$LT$impl$GT$4drop20hc8297115a3f3e3f3KbaE
libwinrt.rlib(winrt.0.o) : error LNK2019: unresolved external symbol WindowsCreateString referenced in function _ZN7hstring13_$LT$impl$GT$3new20h20fb25fe5124490e9daE
libwinrt.rlib(winrt.0.o) : error LNK2019: unresolved external symbol WindowsDeleteString referenced in function _ZN7hstring13_$LT$impl$GT$4drop20hca8ceaa76b908af66eaE

I have added a build.rs build script which sets the library path to find the libraries, and a links = "mincore" to the cargo config, but it is not used.

0 Answers
Related