<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ cargo build
   Compiling libc v0.1.10
   Compiling log v0.3.2
   Compiling link-crash v0.1.0 (file:///C:/Users/%D0%90%D0%BB%D0%B5%D0%BA%D1%81%D0%B0%D0%BD%D0%B4%D1%80/Document
s/link-crash)
error: linking with `link.exe` failed: exit code: 1120
note: &quot;C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC/bin\\link.exe&quot; &quot;/LIBPATH:C:\\Program Files (x86
)\\Microsoft Visual Studio 14.0\\VC/lib\\&quot; &quot;/LIBPATH:C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.10150.
0\\ucrt\\x86&quot; &quot;/LIBPATH:C:\\Program Files (x86)\\Windows Kits\\8.1\\Lib\\winv6.3\\um\\x86&quot; &quot;/NOLOGO&quot; &quot;/NXCOMPAT&quot;
 &quot;/LIBPATH:C:\\Program Files\\rust-nightly-i686-pc-windows-msvc\\rustc\\bin\\rustlib\\i686-pc-windows-msvc\\lib&quot;
 &quot;C:\\Users\\\u{410}\u{43b}\u{435}\u{43a}\u{441}\u{430}\u{43d}\u{434}\u{440}\\Documents\\link-crash\\target\\deb
ug\\link_crash.0.o&quot; &quot;/OUT:C:\\Users\\\u{410}\u{43b}\u{435}\u{43a}\u{441}\u{430}\u{43d}\u{434}\u{440}\\Documents\
\link-crash\\target\\debug\\link_crash.exe&quot; &quot;/OPT:REF,ICF&quot; &quot;/DEBUG&quot; &quot;C:\\Users\\\u{410}\u{43b}\u{435}\u{43a}\u{4
41}\u{430}\u{43d}\u{434}\u{440}\\Documents\\link-crash\\target\\debug\\liblink_crash.rlib&quot; &quot;C:\\Users\\\u{410}\u
{43b}\u{435}\u{43a}\u{441}\u{430}\u{43d}\u{434}\u{440}\\Documents\\link-crash\\target\\debug\\deps\\liblog-f18a3
e885170bd3f.rlib&quot; &quot;C:\\Users\\\u{410}\u{43b}\u{435}\u{43a}\u{441}\u{430}\u{43d}\u{434}\u{440}\\Documents\\link-c
rash\\target\\debug\\deps\\liblibc-144c435538abd757.rlib&quot; &quot;C:\\Program Files\\rust-nightly-i686-pc-windows-msvc\
\rustc\\bin\\rustlib\\i686-pc-windows-msvc\\lib\\libstd-35017696.rlib&quot; &quot;C:\\Program Files\\rust-nightly-i686-pc-
windows-msvc\\rustc\\bin\\rustlib\\i686-pc-windows-msvc\\lib\\libcollections-35017696.rlib&quot; &quot;C:\\Program Files\\
rust-nightly-i686-pc-windows-msvc\\rustc\\bin\\rustlib\\i686-pc-windows-msvc\\lib\\librustc_unicode-35017696.rli
b&quot; &quot;C:\\Program Files\\rust-nightly-i686-pc-windows-msvc\\rustc\\bin\\rustlib\\i686-pc-windows-msvc\\lib\\libran
d-35017696.rlib&quot; &quot;C:\\Program Files\\rust-nightly-i686-pc-windows-msvc\\rustc\\bin\\rustlib\\i686-pc-windows-msv
c\\lib\\liballoc-35017696.rlib&quot; &quot;C:\\Program Files\\rust-nightly-i686-pc-windows-msvc\\rustc\\bin\\rustlib\\i686
-pc-windows-msvc\\lib\\liballoc_system-35017696.rlib&quot; &quot;C:\\Program Files\\rust-nightly-i686-pc-windows-msvc\\rus
tc\\bin\\rustlib\\i686-pc-windows-msvc\\lib\\liblibc-35017696.rlib&quot; &quot;C:\\Program Files\\rust-nightly-i686-pc-win
dows-msvc\\rustc\\bin\\rustlib\\i686-pc-windows-msvc\\lib\\libcore-35017696.rlib&quot; &quot;/LIBPATH:C:\\Users\\\u{410}\u
{43b}\u{435}\u{43a}\u{441}\u{430}\u{43d}\u{434}\u{440}\\Documents\\link-crash\\target\\debug&quot; &quot;/LIBPATH:C:\\User
s\\\u{410}\u{43b}\u{435}\u{43a}\u{441}\u{430}\u{43d}\u{434}\u{440}\\Documents\\link-crash\\target\\debug\\deps&quot;
&quot;/LIBPATH:C:\\Program Files\\rust-nightly-i686-pc-windows-msvc\\rustc\\bin\\rustlib\\i686-pc-windows-msvc\\lib&quot;
&quot;/LIBPATH:C:\\Users\\\u{410}\u{43b}\u{435}\u{43a}\u{441}\u{430}\u{43d}\u{434}\u{440}\\Documents\\link-crash\\.ru
st\\bin\\i686-pc-windows-msvc&quot; &quot;/LIBPATH:C:\\Users\\\u{410}\u{43b}\u{435}\u{43a}\u{441}\u{430}\u{43d}\u{434}\u{4
40}\\Documents\\link-crash\\bin\\i686-pc-windows-msvc&quot; &quot;kernel32.lib&quot; &quot;shell32.lib&quot; &quot;msvcrt.lib&quot; &quot;ws2_32.lib&quot; &quot;u
serenv.lib&quot; &quot;advapi32.lib&quot; &quot;kernel32.lib&quot; &quot;shell32.lib&quot; &quot;msvcrt.lib&quot; &quot;compiler-rt.lib&quot;
Could not compile `link-crash`.

To learn more, run the command again with --verbose."><pre class="notranslate"><code class="notranslate">$ cargo build
   Compiling libc v0.1.10
   Compiling log v0.3.2
   Compiling link-crash v0.1.0 (file:///C:/Users/%D0%90%D0%BB%D0%B5%D0%BA%D1%81%D0%B0%D0%BD%D0%B4%D1%80/Document
s/link-crash)
error: linking with `link.exe` failed: exit code: 1120
note: "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC/bin\\link.exe" "/LIBPATH:C:\\Program Files (x86
)\\Microsoft Visual Studio 14.0\\VC/lib\\" "/LIBPATH:C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.10150.
0\\ucrt\\x86" "/LIBPATH:C:\\Program Files (x86)\\Windows Kits\\8.1\\Lib\\winv6.3\\um\\x86" "/NOLOGO" "/NXCOMPAT"
 "/LIBPATH:C:\\Program Files\\rust-nightly-i686-pc-windows-msvc\\rustc\\bin\\rustlib\\i686-pc-windows-msvc\\lib"
 "C:\\Users\\\u{410}\u{43b}\u{435}\u{43a}\u{441}\u{430}\u{43d}\u{434}\u{440}\\Documents\\link-crash\\target\\deb
ug\\link_crash.0.o" "/OUT:C:\\Users\\\u{410}\u{43b}\u{435}\u{43a}\u{441}\u{430}\u{43d}\u{434}\u{440}\\Documents\
\link-crash\\target\\debug\\link_crash.exe" "/OPT:REF,ICF" "/DEBUG" "C:\\Users\\\u{410}\u{43b}\u{435}\u{43a}\u{4
41}\u{430}\u{43d}\u{434}\u{440}\\Documents\\link-crash\\target\\debug\\liblink_crash.rlib" "C:\\Users\\\u{410}\u
{43b}\u{435}\u{43a}\u{441}\u{430}\u{43d}\u{434}\u{440}\\Documents\\link-crash\\target\\debug\\deps\\liblog-f18a3
e885170bd3f.rlib" "C:\\Users\\\u{410}\u{43b}\u{435}\u{43a}\u{441}\u{430}\u{43d}\u{434}\u{440}\\Documents\\link-c
rash\\target\\debug\\deps\\liblibc-144c435538abd757.rlib" "C:\\Program Files\\rust-nightly-i686-pc-windows-msvc\
\rustc\\bin\\rustlib\\i686-pc-windows-msvc\\lib\\libstd-35017696.rlib" "C:\\Program Files\\rust-nightly-i686-pc-
windows-msvc\\rustc\\bin\\rustlib\\i686-pc-windows-msvc\\lib\\libcollections-35017696.rlib" "C:\\Program Files\\
rust-nightly-i686-pc-windows-msvc\\rustc\\bin\\rustlib\\i686-pc-windows-msvc\\lib\\librustc_unicode-35017696.rli
b" "C:\\Program Files\\rust-nightly-i686-pc-windows-msvc\\rustc\\bin\\rustlib\\i686-pc-windows-msvc\\lib\\libran
d-35017696.rlib" "C:\\Program Files\\rust-nightly-i686-pc-windows-msvc\\rustc\\bin\\rustlib\\i686-pc-windows-msv
c\\lib\\liballoc-35017696.rlib" "C:\\Program Files\\rust-nightly-i686-pc-windows-msvc\\rustc\\bin\\rustlib\\i686
-pc-windows-msvc\\lib\\liballoc_system-35017696.rlib" "C:\\Program Files\\rust-nightly-i686-pc-windows-msvc\\rus
tc\\bin\\rustlib\\i686-pc-windows-msvc\\lib\\liblibc-35017696.rlib" "C:\\Program Files\\rust-nightly-i686-pc-win
dows-msvc\\rustc\\bin\\rustlib\\i686-pc-windows-msvc\\lib\\libcore-35017696.rlib" "/LIBPATH:C:\\Users\\\u{410}\u
{43b}\u{435}\u{43a}\u{441}\u{430}\u{43d}\u{434}\u{440}\\Documents\\link-crash\\target\\debug" "/LIBPATH:C:\\User
s\\\u{410}\u{43b}\u{435}\u{43a}\u{441}\u{430}\u{43d}\u{434}\u{440}\\Documents\\link-crash\\target\\debug\\deps"
"/LIBPATH:C:\\Program Files\\rust-nightly-i686-pc-windows-msvc\\rustc\\bin\\rustlib\\i686-pc-windows-msvc\\lib"
"/LIBPATH:C:\\Users\\\u{410}\u{43b}\u{435}\u{43a}\u{441}\u{430}\u{43d}\u{434}\u{440}\\Documents\\link-crash\\.ru
st\\bin\\i686-pc-windows-msvc" "/LIBPATH:C:\\Users\\\u{410}\u{43b}\u{435}\u{43a}\u{441}\u{430}\u{43d}\u{434}\u{4
40}\\Documents\\link-crash\\bin\\i686-pc-windows-msvc" "kernel32.lib" "shell32.lib" "msvcrt.lib" "ws2_32.lib" "u
serenv.lib" "advapi32.lib" "kernel32.lib" "shell32.lib" "msvcrt.lib" "compiler-rt.lib"
Could not compile `link-crash`.

To learn more, run the command again with --verbose.
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ rustc -vV
rustc 1.4.0-nightly (f3f23bf9c 2015-08-30)
binary: rustc
commit-hash: f3f23bf9c5ee16dab8c25bb5d301d689fbbe0cf1
commit-date: 2015-08-30
host: i686-pc-windows-msvc
release: 1.4.0-nightly"><pre class="notranslate"><code class="notranslate">$ rustc -vV
rustc 1.4.0-nightly (f3f23bf9c 2015-08-30)
binary: rustc
commit-hash: f3f23bf9c5ee16dab8c25bb5d301d689fbbe0cf1
commit-date: 2015-08-30
host: i686-pc-windows-msvc
release: 1.4.0-nightly
</code></pre></div>
<p dir="auto">Here is the minimal test case:</p>
<ul dir="auto">
<li><code class="notranslate">Cargo.toml</code>:</li>
</ul>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="[package]
name = &quot;link-crash&quot;
version = &quot;0.1.0&quot;
authors = [&quot;Alexander Bulaev &lt;alexbool@yandex-team.ru&gt;&quot;]

[dependencies]
log = &quot;*&quot;"><pre class="notranslate"><span class="pl-kos">[</span>package<span class="pl-kos">]</span>
name = <span class="pl-s">"link-crash"</span>
version = <span class="pl-s">"0.1.0"</span>
authors = <span class="pl-kos">[</span><span class="pl-s">"Alexander Bulaev &lt;alexbool@yandex-team.ru&gt;"</span><span class="pl-kos">]</span>

<span class="pl-kos">[</span>dependencies<span class="pl-kos">]</span>
log = <span class="pl-s">"*"</span></pre></div>
<ul dir="auto">
<li><code class="notranslate">src/lib.rs</code>:</li>
</ul>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="#[macro_use]
extern crate log;

use std::fmt::Debug;

pub struct Logger&lt;E: Debug&gt; {
    field: E,
}

impl&lt;E: Debug&gt; Logger&lt;E&gt; {
    pub fn new(field: E) -&gt; Logger&lt;E&gt; {
        Logger {
            field: field
        }
    }

    pub fn do_log(&amp;self) {
        info!(&quot;This is the message: {:?}&quot;, self.field);
    }
}"><pre class="notranslate"><span class="pl-c1">#<span class="pl-kos">[</span>macro_use<span class="pl-kos">]</span></span>
<span class="pl-k">extern</span> <span class="pl-k">crate</span> log<span class="pl-kos">;</span>

<span class="pl-k">use</span> std<span class="pl-kos">::</span>fmt<span class="pl-kos">::</span><span class="pl-v">Debug</span><span class="pl-kos">;</span>

<span class="pl-k">pub</span> <span class="pl-k">struct</span> <span class="pl-smi">Logger</span><span class="pl-kos">&lt;</span><span class="pl-smi">E</span><span class="pl-kos">:</span> <span class="pl-smi">Debug</span><span class="pl-kos">&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-c1">field</span><span class="pl-kos">:</span> <span class="pl-smi">E</span><span class="pl-kos">,</span>
<span class="pl-kos">}</span>

<span class="pl-k">impl</span><span class="pl-kos">&lt;</span><span class="pl-smi">E</span><span class="pl-kos">:</span> <span class="pl-smi">Debug</span><span class="pl-kos">&gt;</span> <span class="pl-smi">Logger</span><span class="pl-kos">&lt;</span><span class="pl-smi">E</span><span class="pl-kos">&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-k">pub</span> <span class="pl-k">fn</span> <span class="pl-en">new</span><span class="pl-kos">(</span><span class="pl-s1">field</span><span class="pl-kos">:</span> <span class="pl-smi">E</span><span class="pl-kos">)</span> -&gt; <span class="pl-smi">Logger</span><span class="pl-kos">&lt;</span><span class="pl-smi">E</span><span class="pl-kos">&gt;</span> <span class="pl-kos">{</span>
        <span class="pl-smi">Logger</span> <span class="pl-kos">{</span>
            <span class="pl-c1">field</span><span class="pl-kos">:</span> field
        <span class="pl-kos">}</span>
    <span class="pl-kos">}</span>

    <span class="pl-k">pub</span> <span class="pl-k">fn</span> <span class="pl-en">do_log</span><span class="pl-kos">(</span><span class="pl-c1">&amp;</span><span class="pl-smi">self</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-en">info</span><span class="pl-en">!</span><span class="pl-kos">(</span><span class="pl-s">"This is the message: {:?}"</span>, <span class="pl-smi">self</span>.field<span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<ul dir="auto">
<li><code class="notranslate">src/main.rs</code>:</li>
</ul>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="extern crate link_crash;

fn main() {
    link_crash::Logger::new(&quot;123&quot;).do_log();
}"><pre class="notranslate"><span class="pl-k">extern</span> <span class="pl-k">crate</span> link_crash<span class="pl-kos">;</span>

<span class="pl-k">fn</span> <span class="pl-en">main</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    link_crash<span class="pl-kos">::</span><span class="pl-smi">Logger</span><span class="pl-kos">::</span><span class="pl-en">new</span><span class="pl-kos">(</span><span class="pl-s">"123"</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">do_log</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">Currently the compiler makes basically no attempt to correctly use <code class="notranslate">dllimport</code>. As a bit of a refresher, the Windows linker requires that if you're importing symbols from a DLL that they're tagged with <code class="notranslate">dllimport</code>. This helps wire things up correctly at runtime and link-time. To help us out, though, the linker will patch up a few cases where <code class="notranslate">dllimport</code> is missing where it would otherwise be required. If a function in another DLL is linked to without <code class="notranslate">dllimport</code> then the linker will inject a local shim which adds a bit of indirection and runtime overhead but allows the crate to link correctly. For importing constants from other DLLs, however, MSVC linker requires that dllimport is annotated correctly. MinGW linkers can sometimes workaround it (see <a href="https://github.com/llvm/llvm-project/commit/eac1b05f1db91097eb37d975e07789ce039cf7f7">this commit</a> description.</p>
<p dir="auto">If we're targeting windows, then the compiler currently puts <code class="notranslate">dllimport</code> on all imported constants from external crates, regardless of whether it's actually being imported from another crate. We rely on the linker fixing up all imports of functions. This ends up meaning that some crates don't link correctly, however (see this comment: <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="91209293" data-permission-text="Title is private" data-url="https://github.com/rust-lang/rust/issues/26591" data-hovercard-type="issue" data-hovercard-url="/rust-lang/rust/issues/26591/hovercard?comment_id=123513631&amp;comment_type=issue_comment" href="https://github.com/rust-lang/rust/issues/26591#issuecomment-123513631">#26591 (comment)</a>).</p>
<p dir="auto">We should fix the compiler's handling of dllimport in a few ways:</p>
<ul dir="auto">
<li>Functions should be tagged with <code class="notranslate">dllimport</code> where appropriate</li>
<li>FFI functions should also be tagged with <code class="notranslate">dllimport</code> where appropriate</li>
<li>Constants should not always be tagged with <code class="notranslate">dllimport</code> if they're not actually being imported from a DLL.</li>
</ul>
<p dir="auto">I currently have a few thoughts running around in my head for fixing this, but nothing seems plausible enough to push on.</p>
<p dir="auto">EDIT: Updated as <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/mati865/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/mati865">@mati865</a> requested <a href="https://github.com/rust-lang/rust/issues/27438#issuecomment-665739234" data-hovercard-type="issue" data-hovercard-url="/rust-lang/rust/issues/27438/hovercard">here</a>.</p>