Windows-rs: Linker error when trying to use DirectX

Created on 25 Jan 2021  路  3Comments  路  Source: microsoft/windows-rs

I am trying to use the DirectML api by translating this example to Rust. Unfortunately I am getting a linker error:

error: linking with `link.exe` failed: exit code: 1181
  |
  = note: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\VC\\Tools\\MSVC\\14.28.29333\\bin\\HostX64\\x64\\link.exe" "/NOLOGO" "/NXCOMPAT" "/LIBPATH:C:\\Users\\johan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\hello_direct_ml.19ucnf7fc0tabxba.rcgu.o" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\hello_direct_ml.1hxhhh1ddxhdcyir.rcgu.o" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\hello_direct_ml.1w9se521sb86uxfb.rcgu.o" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\hello_direct_ml.1z11h8mxewjk0ess.rcgu.o" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\hello_direct_ml.22c9nk7scgaxzlu.rcgu.o" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\hello_direct_ml.2q3zz691mjc3vbqh.rcgu.o" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\hello_direct_ml.2zdc39v2h5og4awn.rcgu.o" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\hello_direct_ml.39zxokbwrd4a584b.rcgu.o" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\hello_direct_ml.3jmfidopopkon3ov.rcgu.o" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\hello_direct_ml.3o6n6b1kz5s6fl61.rcgu.o" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\hello_direct_ml.4xak62askuidjeo5.rcgu.o" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\hello_direct_ml.5i4k6192t1bg6il.rcgu.o" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\hello_direct_ml.8q9mej3rchq8w0m.rcgu.o" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\hello_direct_ml.j0tqotgsbkmrrqv.rcgu.o" "/OUT:D:\\repos\\hello_direct_ml\\target\\debug\\deps\\hello_direct_ml.exe" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\hello_direct_ml.26idl5edcgp8psaw.rcgu.o" "/OPT:REF,NOICF" "/DEBUG" "/NATVIS:C:\\Users\\johan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\intrinsic.natvis" "/NATVIS:C:\\Users\\johan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\liballoc.natvis" "/NATVIS:C:\\Users\\johan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libcore.natvis" "/NATVIS:C:\\Users\\johan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libstd.natvis" "/LIBPATH:D:\\repos\\hello_direct_ml\\target\\debug\\deps" "/LIBPATH:C:\\Users\\johan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "d3d11.lib" "d3d11.lib" "D3DCOMPILER_47.lib" "d3dcsx.lib" "d3dcsx.lib" "d3dcsx.lib" "d3dcsx.lib" "d3dcsx.lib" "d3dcsx.lib" "d3dcsx.lib" "d3dcsx.lib" "d3dcsx.lib" "d3d11.lib" "d3d12.lib" "d3d12.lib" "d3d12.lib" "d3d12.lib" "d3d12.lib" "d3d12.lib" "d3d12.lib" "DirectML.lib" "DirectML.lib" "dxgi.lib" "dxgi.lib" "dxgi.lib" "dxgi.lib" "dxgi.lib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\libwindows-c8903a935f0c1d0f.rlib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\libconst_sha1-bb6f3288d0242592.rlib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\libwindows_gen-9718571582a41fc6.rlib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\librayon-7908d7a6c29f8f87.rlib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\librayon_core-0cf7c31029bf6e8c.rlib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\libnum_cpus-bbec145b1404c915.rlib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\libcrossbeam_deque-594c841d11abdb36.rlib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\libcrossbeam_epoch-e7609e47dd7a4d72.rlib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\libmemoffset-a9e6e83e88acd2da.rlib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\libscopeguard-189d2fb69761c2d2.rlib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\libcrossbeam_channel-f571eea540c0a54e.rlib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\libcrossbeam_utils-94e9c9317b819eac.rlib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\liblazy_static-d8f2cce8f9c0e45e.rlib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\libcfg_if-d8338c77c5c396c1.rlib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\libeither-00921ea79704f0ac.rlib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\libsquote-8e062441de2d995e.rlib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\libwindows_winmd-cb7194a783a1b52e.rlib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\libserde_json-7db8c81d536626e6.rlib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\libryu-2f8ee480d447e5ad.rlib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\libitoa-240a36e80d20424e.rlib" "D:\\repos\\hello_direct_ml\\target\\debug\\deps\\libserde-e09a20ca46287b78.rlib" "C:\\Users\\johan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libstd-1feb4ba9912f83e4.rlib" "C:\\Users\\johan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libpanic_unwind-10caf631bf17818d.rlib" "C:\\Users\\johan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_demangle-5f5b841e7dcb5069.rlib" "C:\\Users\\johan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libhashbrown-886e42042440a542.rlib" "C:\\Users\\johan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_std_workspace_alloc-fc3dfd2deda68757.rlib" "C:\\Users\\johan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libunwind-4765baa3d9fc6a1b.rlib" "C:\\Users\\johan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcfg_if-2af04b7075550e2b.rlib" "C:\\Users\\johan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liblibc-9f4eae3434a19b51.rlib" "C:\\Users\\johan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liballoc-14b08c3097e998dc.rlib" "C:\\Users\\johan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_std_workspace_core-9c0450bb353ef0cc.rlib" "C:\\Users\\johan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcore-4856f32e5e48bded.rlib" "C:\\Users\\johan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcompiler_builtins-0f66c8d6b2ebbbc4.rlib" "oleaut32.lib" "kernel32.lib" "oleaut32.lib" "kernel32.lib" "ole32.lib" "kernel32.lib" "kernel32.lib" "kernel32.lib" "oleaut32.lib" "advapi32.lib" "ws2_32.lib" "userenv.lib" "msvcrt.lib" "/NODEFAULTLIB:LIBCMT"
  = note: LINK : fatal error LNK1181: cannot open input file 'D3DCOMPILER_47.lib'

In the generated bindings file windows.rs I noticed the following:

            #[link(name = "D3DCOMPILER_47")]
            extern "system" {
                pub fn D3DDisassemble11Trace(
                    p_src_data: *mut ::std::ffi::c_void,
                    src_data_size: usize,
                    p_trace: ::std::option::Option<ID3D11ShaderTrace>,
                    start_step: u32,
                    num_steps: u32,
                    flags: u32,
                    pp_disassembly: *mut ::std::option::Option<ID3DBlob>,
                ) -> ::windows::ErrorCode;
            }

There is a d3dcompiler_47.dll under windows\system32. Not sure where the d3dcompiler_47.lib is expected to be.
Is there a specific version of the Windows SDK that should be installed?

Here are the bindings I am trying to use:

fn main() {
    windows::build!(
        windows::win32::direct3d11::*
        windows::win32::direct3d12::*
        windows::win32::dxgi::*
        windows::win32::direct_ml::*
    );
}

Here is a minimal failing test:

#[cfg(test)]
mod tests {
    use super::Runtime;
    use crate::bindings::{
        windows::win32::direct3d11::*,
        windows::win32::direct3d12::*,
        windows::win32::direct_ml::*,
        windows::win32::dxgi::*,
    };

    use windows::Interface;
    #[test]
    fn can_create_factory() {
        let mut dxgi_factory:Option<IDXGIFactory> = None;
        unsafe {
            CreateDXGIFactory1(
                &IDXGIFactory4::IID,
                &mut dxgi_factory as *mut _ as _).ok().expect("expecting a dxgi factory.")
        };

        assert!(dxgi_factory.is_some());
        let mut dxgi_adapter = None;
        unsafe {
            dxgi_factory.unwrap().EnumAdapters(
                0, &mut dxgi_adapter as *mut _ as _).ok().expect("Expecting an adapter.")
        };
        assert!(dxgi_adapter.is_some());
    }

}
blocked bug

Most helpful comment

A more comprehensive workaround is now in place to deal with the linker issue. You should now be able to use this function without issue.

All 3 comments

The metadata indicates the name of the DLL that exports the given function, but Rust doesn't yet support DLL imports and expects a LIB file to resolve those imports. If you have the Windows SDK installed, there will usually be a LIB file by the right name that Rust can use but this is not guaranteed. There are a few edge cases like this one where this doesn't quite line up. I considered generating my own LIB files for the windows crate #142 but we have some folks looking into implementing DLL imports for Rust and I'm hopeful that will arrive soon.

I have pinned the following issue describing the problem as it is a common question: https://github.com/microsoft/windows-rs/issues/463

A more comprehensive workaround is now in place to deal with the linker issue. You should now be able to use this function without issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tim-weis picture tim-weis  路  3Comments

kennykerr picture kennykerr  路  5Comments

rylev picture rylev  路  5Comments

rylev picture rylev  路  5Comments

DJankauskas picture DJankauskas  路  3Comments