Skip to content

Tests fail to link on Windows #1008

@MStarha

Description

@MStarha

In an embedded project I have two packages in the same workspace: one with FW and one with unit tests to be run on host. On Linux (Fedora 42) everything works fine, but on Windows 11 the unit tests fail to link (but the FW build succeeds).

The embedded target is thumbv7m-none-eabi, the Linux target is x86_64-unknown-linux-gnu, the Win target is x86_64-pc-windows-gnu (I do think I even tried x86_64-pc-windows-msvc with the same result). Rust toolchain version is 1.89.0.

There is a workaround using cross: cross test --target x86_64-unknown-linux-gnu, provided Docker or similar is installed (see cross' documentation).

Let me know if there is any more information you'd need.

Here is a MRE. Note there is no need to actually use any of the defmt functionality:

#[derive(Debug, Clone, Copy, PartialEq, Eq, defmt::Format)]
pub enum Test {
    A,
    B,
}

#[cfg(test)]
mod test {
    use crate::Test;

    #[test]
    fn test() {
        let _a = Test::A;
    }
}

Cargo.toml:

[package]
name = "defmt-test"
version = "0.1.0"
edition = "2024"

[dependencies]
defmt = "1.0.1"

Here is the error it produces:

cargo test
   Compiling defmt-test v0.1.0 (C:\Data\defmt-test)
error: linking with `x86_64-w64-mingw32-gcc` failed: exit code: 1
  |
  = note: "x86_64-w64-mingw32-gcc" "-fno-use-linker-plugin" "-Wl,--dynamicbase" "-Wl,--disable-auto-image-base" "-m64" "-Wl,--high-entropy-va" "<sysroot>\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsbegin.o" "C:\\Users\\MF1E3~1.STA\\AppData\\Local\\Temp\\rustcyGL6E2\\symbols.o" "<18 object files omitted>" "-Wl,-Bstatic" "<sysroot>\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib/{libtest-*,libgetopts-*,libunicode_width-*,librustc_std_workspace_std-*}.rlib" "C:\\Data\\defmt-test\\target\\debug\\deps/{libdefmt-5d981f2caefd801c.rlib,libbitflags-bd5a2d67793357cf.rlib}.rlib" "<sysroot>\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib/{libstd-*,libpanic_unwind-*,libobject-*,libmemchr-*,libaddr2line-*,libgimli-*,libwindows_targets-*,librustc_demangle-*,libstd_detect-*,libhashbrown-*,librustc_std_workspace_alloc-*,libminiz_oxide-*,libadler2-*,libunwind-*,libcfg_if-*,liblibc-*,librustc_std_workspace_core-*,liballoc-*,libcore-*,libcompiler_builtins-*}.rlib" "-Wl,-Bdynamic" "-lkernel32" "-lkernel32" "-lkernel32" "-lkernel32" "-lntdll" "-luserenv" "-lws2_32" "-ldbghelp" "-lgcc_eh" "-l:libpthread.a" "-lmsvcrt" "-lmingwex" "-lmingw32" "-lgcc" "-lmsvcrt" "-lmingwex" "-luser32" "-lkernel32" "-Wl,--nxcompat" "-L" "C:\\Data\\defmt-test\\target\\debug\\build\\defmt-3e3b414c6bab945e\\out" "-o" "C:\\Data\\defmt-test\\target\\debug\\deps\\defmt_test-814b792f171f99e0.exe" "-Wl,--gc-sections" "-no-pie" "-nodefaultlibs" "<sysroot>\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsend.o"
  = note: some arguments are omitted. use `--verbose` to show all linker arguments
  = note: C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Data\defmt-test\target\debug\deps\libdefmt-5d981f2caefd801c.rlib(defmt-5d981f2caefd801c.defmt.561ef4f777820981-cgu.0.rcgu.o): in function `defmt::export::write':␍
          C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\defmt-1.0.1/src\export/mod.rs:85:(.text+0xc0d): undefined reference to `_defmt_write'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\defmt-1.0.1/src\export/mod.rs:85:(.text+0xc5d): undefined reference to `_defmt_write'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\defmt-1.0.1/src\export/mod.rs:85:(.text+0xdb3): undefined reference to `_defmt_write'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\defmt-1.0.1/src\export/mod.rs:85:(.text+0xe07): undefined reference to `_defmt_write'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\defmt-1.0.1/src\export/mod.rs:85:(.text+0xe5a): undefined reference to `_defmt_write'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Data\defmt-test\target\debug\deps\libdefmt-5d981f2caefd801c.rlib(defmt-5d981f2caefd801c.defmt.561ef4f777820981-cgu.0.rcgu.o):C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\defmt-1.0.1/src\export/mod.rs:85: more undefined references to `_defmt_write' follow␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Data\defmt-test\target\debug\deps\libdefmt-5d981f2caefd801c.rlib(defmt-5d981f2caefd801c.defmt.561ef4f777820981-cgu.0.rcgu.o): in function `defmt::export::acquire':␍
          C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\defmt-1.0.1/src\export/mod.rs:55:(.text+0x111f): undefined reference to `_defmt_acquire'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Data\defmt-test\target\debug\deps\libdefmt-5d981f2caefd801c.rlib(defmt-5d981f2caefd801c.defmt.561ef4f777820981-cgu.0.rcgu.o): in function `defmt::export::timestamp':␍
          C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\defmt-1.0.1/src\export/mod.rs:98:(.text+0x1133): undefined reference to `_defmt_timestamp'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Data\defmt-test\target\debug\deps\libdefmt-5d981f2caefd801c.rlib(defmt-5d981f2caefd801c.defmt.561ef4f777820981-cgu.0.rcgu.o): in function `defmt::export::acquire':␍
          C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\defmt-1.0.1/src\export/mod.rs:55:(.text+0x114f): undefined reference to `_defmt_acquire'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Data\defmt-test\target\debug\deps\libdefmt-5d981f2caefd801c.rlib(defmt-5d981f2caefd801c.defmt.561ef4f777820981-cgu.0.rcgu.o): in function `defmt::export::timestamp':␍
          C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\defmt-1.0.1/src\export/mod.rs:98:(.text+0x1163): undefined reference to `_defmt_timestamp'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Data\defmt-test\target\debug\deps\libdefmt-5d981f2caefd801c.rlib(defmt-5d981f2caefd801c.defmt.561ef4f777820981-cgu.0.rcgu.o): in function `defmt::export::release':␍
          C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\defmt-1.0.1/src\export/mod.rs:71:(.text+0x1168): undefined reference to `_defmt_release'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Data\defmt-test\target\debug\deps\libdefmt-5d981f2caefd801c.rlib(defmt-5d981f2caefd801c.defmt.561ef4f777820981-cgu.0.rcgu.o): in function `defmt::export::write':␍
          C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\defmt-1.0.1/src\export/mod.rs:85:(.text+0x11c2): undefined reference to `_defmt_write'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Data\defmt-test\target\debug\deps\libdefmt-5d981f2caefd801c.rlib(defmt-5d981f2caefd801c.defmt.561ef4f777820981-cgu.0.rcgu.o): in function `defmt::flush':␍
          C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\defmt-1.0.1/src/lib.rs:415:(.text+0x15d5): undefined reference to `_defmt_acquire'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\defmt-1.0.1/src/lib.rs:416:(.text+0x15da): undefined reference to `_defmt_flush'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\defmt-1.0.1/src/lib.rs:417:(.text+0x15df): undefined reference to `_defmt_release'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Data\defmt-test\target\debug\deps\libdefmt-5d981f2caefd801c.rlib(defmt-5d981f2caefd801c.defmt.561ef4f777820981-cgu.0.rcgu.o): in function `defmt::export::write':␍
          C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\defmt-1.0.1/src\export/mod.rs:85:(.text+0x172a): undefined reference to `_defmt_write'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\defmt-1.0.1/src\export/mod.rs:85:(.text+0x177e): undefined reference to `_defmt_write'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\defmt-1.0.1/src\export/mod.rs:85:(.text+0x17ca): undefined reference to `_defmt_write'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\defmt-1.0.1/src\export/mod.rs:85:(.text+0x181e): undefined reference to `_defmt_write'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\defmt-1.0.1/src\export/mod.rs:85:(.text+0x1868): undefined reference to `_defmt_write'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Data\defmt-test\target\debug\deps\libdefmt-5d981f2caefd801c.rlib(defmt-5d981f2caefd801c.defmt.561ef4f777820981-cgu.0.rcgu.o):C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\defmt-1.0.1/src\export/mod.rs:85: more undefined references to `_defmt_write' follow␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Data\defmt-test\target\debug\deps\libdefmt-5d981f2caefd801c.rlib(defmt-5d981f2caefd801c.defmt.561ef4f777820981-cgu.0.rcgu.o):defmt.561ef4f777820981-cgu.0:(.rdata$.refptr.__DEFMT_MARKER_DEBUG_END[.refptr.__DEFMT_MARKER_DEBUG_END]+0x0): undefined reference to `__DEFMT_MARKER_DEBUG_END'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Data\defmt-test\target\debug\deps\libdefmt-5d981f2caefd801c.rlib(defmt-5d981f2caefd801c.defmt.561ef4f777820981-cgu.0.rcgu.o):defmt.561ef4f777820981-cgu.0:(.rdata$.refptr.__DEFMT_MARKER_DEBUG_START[.refptr.__DEFMT_MARKER_DEBUG_START]+0x0): undefined reference to `__DEFMT_MARKER_DEBUG_START'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Data\defmt-test\target\debug\deps\libdefmt-5d981f2caefd801c.rlib(defmt-5d981f2caefd801c.defmt.561ef4f777820981-cgu.0.rcgu.o):defmt.561ef4f777820981-cgu.0:(.rdata$.refptr.__DEFMT_MARKER_ERROR_END[.refptr.__DEFMT_MARKER_ERROR_END]+0x0): undefined reference to `__DEFMT_MARKER_ERROR_END'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Data\defmt-test\target\debug\deps\libdefmt-5d981f2caefd801c.rlib(defmt-5d981f2caefd801c.defmt.561ef4f777820981-cgu.0.rcgu.o):defmt.561ef4f777820981-cgu.0:(.rdata$.refptr.__DEFMT_MARKER_ERROR_START[.refptr.__DEFMT_MARKER_ERROR_START]+0x0): undefined reference to `__DEFMT_MARKER_ERROR_START'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Data\defmt-test\target\debug\deps\libdefmt-5d981f2caefd801c.rlib(defmt-5d981f2caefd801c.defmt.561ef4f777820981-cgu.0.rcgu.o):defmt.561ef4f777820981-cgu.0:(.rdata$.refptr.__DEFMT_MARKER_INFO_END[.refptr.__DEFMT_MARKER_INFO_END]+0x0): undefined reference to `__DEFMT_MARKER_INFO_END'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Data\defmt-test\target\debug\deps\libdefmt-5d981f2caefd801c.rlib(defmt-5d981f2caefd801c.defmt.561ef4f777820981-cgu.0.rcgu.o):defmt.561ef4f777820981-cgu.0:(.rdata$.refptr.__DEFMT_MARKER_INFO_START[.refptr.__DEFMT_MARKER_INFO_START]+0x0): undefined reference to `__DEFMT_MARKER_INFO_START'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Data\defmt-test\target\debug\deps\libdefmt-5d981f2caefd801c.rlib(defmt-5d981f2caefd801c.defmt.561ef4f777820981-cgu.0.rcgu.o):defmt.561ef4f777820981-cgu.0:(.rdata$.refptr.__DEFMT_MARKER_TRACE_END[.refptr.__DEFMT_MARKER_TRACE_END]+0x0): undefined reference to `__DEFMT_MARKER_TRACE_END'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Data\defmt-test\target\debug\deps\libdefmt-5d981f2caefd801c.rlib(defmt-5d981f2caefd801c.defmt.561ef4f777820981-cgu.0.rcgu.o):defmt.561ef4f777820981-cgu.0:(.rdata$.refptr.__DEFMT_MARKER_TRACE_START[.refptr.__DEFMT_MARKER_TRACE_START]+0x0): undefined reference to `__DEFMT_MARKER_TRACE_START'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Data\defmt-test\target\debug\deps\libdefmt-5d981f2caefd801c.rlib(defmt-5d981f2caefd801c.defmt.561ef4f777820981-cgu.0.rcgu.o):defmt.561ef4f777820981-cgu.0:(.rdata$.refptr.__DEFMT_MARKER_WARN_END[.refptr.__DEFMT_MARKER_WARN_END]+0x0): undefined reference to `__DEFMT_MARKER_WARN_END'␍
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Data\defmt-test\target\debug\deps\libdefmt-5d981f2caefd801c.rlib(defmt-5d981f2caefd801c.defmt.561ef4f777820981-cgu.0.rcgu.o):defmt.561ef4f777820981-cgu.0:(.rdata$.refptr.__DEFMT_MARKER_WARN_START[.refptr.__DEFMT_MARKER_WARN_START]+0x0): undefined reference to `__DEFMT_MARKER_WARN_START'␍
          collect2.exe: error: ld returned 1 exit status

  = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#rustc-link-lib)

error: could not compile `defmt-test` (lib test) due to 1 previous error

Here it is running fine with cross:

PS C:\Data\defmt-test> cross test --target x86_64-unknown-linux-gnu
using newer rustc `1.90.0 (1159e78c4 2025-09-14)` for the target. Current active rustc on the host is `rustc 1.89.0 (29483883e 2025-08-04)`.
 > Update with `rustup update`
   Compiling proc-macro2 v1.0.101
   Compiling quote v1.0.41
   Compiling unicode-ident v1.0.19
   Compiling thiserror v2.0.17
   Compiling defmt-macros v1.0.1
   Compiling defmt v1.0.1
   Compiling bitflags v1.3.2
   Compiling syn v2.0.106
   Compiling proc-macro-error-attr2 v2.0.0
   Compiling proc-macro-error2 v2.0.1
   Compiling thiserror-impl v2.0.17
   Compiling defmt-parser v1.0.0
   Compiling defmt-test v0.1.0 (/mnt/c/Data/defmt-test)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 47.94s
     Running unittests src/lib.rs (/target/x86_64-unknown-linux-gnu/debug/deps/defmt_test-726444c6b6f05fcb)

running 1 test
test test::test ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

   Doc-tests defmt_test

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions