Skip to content

link error: undefined symbol in Window msvc : link dynamic library #1610

@sunflower-knight

Description

@sunflower-knight

rustc 1.94.0-nightly (37aa2135b 2025-12-08)

.cargo/config.toml

opt-level = 0              # 关闭优化 加快编译

panic = "unwind" # 开启 panic 处理 -> 避免运行时错误

lto = "off"             # 禁用链接时优化 

[target.x86_64-pc-windows-msvc]
linker = "rust-lld.exe" 


codegen-backend = "cranelift" 
rustflags = [
    "-C",
    "prefer-dynamic", 
  1. If use msvc linker rather than rust-lld , the error still exist
  2. If i don't use "prefer-dynamic", the error disappear
use std::io::{self, Write};

fn main() {
 
    eprintln!("This is an error message to stderr");

 
    let mut stderr = io::stderr();
    writeln!(stderr, "Another stderr message").expect("Failed to write to stderr");

 
    println!("This is normal output to stdout");
}

Link Error

error: linking with `rust-lld.exe` failed: exit code: 1
  |
  = note: "rust-lld.exe" "-flavor" "link" "/NOLOGO" "D:\\Temp\\rustcSoEzPa\\symbols.o" "<15 object files omitted>" "<sysroot>\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\std-4e1e2ae26c8967c0.dll.lib" "<sysroot>\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib/libcompiler_builtins-*.rlib" "kernel32.lib" "kernel32.lib" "kernel32.lib" "ntdll.lib" "userenv.lib" "ws2_32.lib" "dbghelp.lib" "/defaultlib:msvcrt" "D:\\Temp\\rustcSoEzPa\\api-ms-win-core-synch-l1-2-0.dll_imports_indirect.lib" "D:\\Temp\\rustcSoEzPa\\bcryptprimitives.dll_imports_indirect.lib" "/NXCOMPAT" "/OUT:D:\\project\\target\\debug\\deps\\project.exe" "/OPT:REF,NOICF" "/DEBUG" "/PDBALTPATH:%_PDB%" "/NATVIS:<sysroot>\\lib\\rustlib\\etc\\intrinsic.natvis" "/NATVIS:<sysroot>\\lib\\rustlib\\etc\\liballoc.natvis" "/NATVIS:<sysroot>\\lib\\rustlib\\etc\\libcore.natvis" "/NATVIS:<sysroot>\\lib\\rustlib\\etc\\libstd.natvis"
  = note: some arguments are omitted. use `--verbose` to show all linker arguments
  = note: rust-lld: error: undefined symbol: std::io::stdio::stderr::INSTANCE
          >>> referenced by D:\project\target\debug\deps\project.5rcvp1v3l83aolc4wpj6y2vwk.19skkgm.rcgu.o:(.refptr._RNvNvNtNtCsaVqoImmYiGb_3std2io5stdio6stderr8INSTANCE)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.O-windowsOperating system: Windows

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions