我正在尝试在库中使用Rust的WinRT函数。当我创建一个简单的示例程序,它声明外部函数并使用它们编译和链接:
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();
}
}
但是,当我尝试创建一个封装WinRT API的库时,我从链接器获得了未解析的引用。我有一个库winrt
,其中包含
roapi
#[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>
我认为link
属性会指定每当链接lib时,mincore
都是依赖项,并且也是链接的。当我现在创建一个使用此库的示例程序时,不使用mincore
依赖项:
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
我添加了一个build.rs
构建脚本,用于设置库路径以查找库,以及links = "mincore"
到货物配置,但不使用它。