defmt icon indicating copy to clipboard operation
defmt copied to clipboard

error LNK2019: unresolved external symbol _defmt_panic referenced in function

Open haata opened this issue 2 years ago • 18 comments

This is likely similar to #411

I have a reproducible case (only affects Windows).

  • Nightly (cargo 1.63.0-nightly (a4c1cd0eb 2022-05-18))
  • https://github.com/kiibohd/kiibohd-core/tree/main/kll-core (exact hash https://github.com/kiibohd/kiibohd-core/tree/528672a0f7f255eb95cda7fd5423cfc553fa959e)
  • cargo run works
  • cargo test
  = note: libkll_hid-48b2e4c1faf634c7.rlib(kll_hid-48b2e4c1faf634c7.8ek79hmbmne36vd.rcgu.o) : error LNK2019: unresolved external symbol _defmt_panic referenced in function _ZN57_$LT$kll_hid..Locale$u20$as$u20$defmt..traits..Format$GT$6format17h16cbf08301323530E
          libkll_hid-48b2e4c1faf634c7.rlib(kll_hid-48b2e4c1faf634c7.8ek79hmbmne36vd.rcgu.o) : error LNK2019: unresolved external symbol _defmt_acquire referenced in function _ZN57_$LT$kll_hid..Locale$u20$as$u20$defmt..traits..Format$GT$6format17h16cbf08301323530E
          libdefmt-84a1edf1a654ad5f.rlib(defmt-84a1edf1a654ad5f.defmt.6fd46b78-cgu.8.rcgu.o) : error LNK2001: unresolved external symbol _defmt_acquire
          libkll_hid-48b2e4c1faf634c7.rlib(kll_hid-48b2e4c1faf634c7.8ek79hmbmne36vd.rcgu.o) : error LNK2019: unresolved external symbol _defmt_release referenced in function _ZN57_$LT$kll_hid..Locale$u20$as$u20$defmt..traits..Format$GT$6format17h16cbf08301323530E
          libdefmt-84a1edf1a654ad5f.rlib(defmt-84a1edf1a654ad5f.defmt.6fd46b78-cgu.8.rcgu.o) : error LNK2001: unresolved external symbol _defmt_release
          libdefmt-84a1edf1a654ad5f.rlib(defmt-84a1edf1a654ad5f.defmt.6fd46b78-cgu.8.rcgu.o) : error LNK2019: unresolved external symbol _defmt_flush referenced in function _ZN5defmt5flush17h7ba29d74f1244836E
          libdefmt-84a1edf1a654ad5f.rlib(defmt-84a1edf1a654ad5f.defmt.6fd46b78-cgu.3.rcgu.o) : error LNK2019: unresolved external symbol _defmt_write referenced in function _ZN5defmt6export3f3217hed4f57cccfafeed4E
          libdefmt-84a1edf1a654ad5f.rlib(defmt-84a1edf1a654ad5f.defmt.6fd46b78-cgu.6.rcgu.o) : error LNK2001: unresolved external symbol _defmt_write
          libdefmt-84a1edf1a654ad5f.rlib(defmt-84a1edf1a654ad5f.defmt.6fd46b78-cgu.3.rcgu.o) : error LNK2019: unresolved external symbol _defmt_timestamp referenced in function _ZN5defmt6export6header17h47dc426ec968adf6E
          C:\Users\tripl\source\repos\kiibohd\kiibohd-core\target\debug\deps\kll_core-bd87741b11f4a13f.exe : fatal error LNK1120: 6 unresolved externals

I likely can resolve this if I make defmt an optional opt-in dependency.

haata avatar May 23 '22 03:05 haata

An even smaller example (same git repo, also only a problem on Windows).

  • https://github.com/kiibohd/kiibohd-core/tree/528672a0f7f255eb95cda7fd5423cfc553fa959e/kll-hid
  • cargo test
  = note: libkll_hid-48b2e4c1faf634c7.rlib(kll_hid-48b2e4c1faf634c7.8ek79hmbmne36vd.rcgu.o) : error LNK2019: unresolved external symbol _defmt_panic referenced in function _ZN57_$LT$kll_hid..Locale$u20$as$u20$defmt..traits..Format$GT$6format17h16cbf08301323530E
          libkll_hid-48b2e4c1faf634c7.rlib(kll_hid-48b2e4c1faf634c7.8ek79hmbmne36vd.rcgu.o) : error LNK2019: unresolved external symbol _defmt_acquire referenced in function _ZN57_$LT$kll_hid..Locale$u20$as$u20$defmt..traits..Format$GT$6format17h16cbf08301323530E
          libdefmt-84a1edf1a654ad5f.rlib(defmt-84a1edf1a654ad5f.defmt.6fd46b78-cgu.8.rcgu.o) : error LNK2001: unresolved external symbol _defmt_acquire
          libkll_hid-48b2e4c1faf634c7.rlib(kll_hid-48b2e4c1faf634c7.8ek79hmbmne36vd.rcgu.o) : error LNK2019: unresolved external symbol _defmt_release referenced in function _ZN57_$LT$kll_hid..Locale$u20$as$u20$defmt..traits..Format$GT$6format17h16cbf08301323530E
          libdefmt-84a1edf1a654ad5f.rlib(defmt-84a1edf1a654ad5f.defmt.6fd46b78-cgu.8.rcgu.o) : error LNK2001: unresolved external symbol _defmt_release
          libdefmt-84a1edf1a654ad5f.rlib(defmt-84a1edf1a654ad5f.defmt.6fd46b78-cgu.8.rcgu.o) : error LNK2019: unresolved external symbol _defmt_flush referenced in function _ZN5defmt5flush17h7ba29d74f1244836E
          libdefmt-84a1edf1a654ad5f.rlib(defmt-84a1edf1a654ad5f.defmt.6fd46b78-cgu.3.rcgu.o) : error LNK2019: unresolved external symbol _defmt_write referenced in function _ZN5defmt6export3f3217hed4f57cccfafeed4E
          libdefmt-84a1edf1a654ad5f.rlib(defmt-84a1edf1a654ad5f.defmt.6fd46b78-cgu.6.rcgu.o) : error LNK2001: unresolved external symbol _defmt_write
          libdefmt-84a1edf1a654ad5f.rlib(defmt-84a1edf1a654ad5f.defmt.6fd46b78-cgu.3.rcgu.o) : error LNK2019: unresolved external symbol _defmt_timestamp referenced in function _ZN5defmt6export6header17h47dc426ec968adf6E
          C:\Users\tripl\source\repos\kiibohd\kiibohd-core\target\debug\deps\kll_core-bd87741b11f4a13f.exe : fatal error LNK1120: 6 unresolved externals
          ```

haata avatar May 23 '22 04:05 haata

Thank you for reporting, we will look into it.

Urhengulas avatar May 31 '22 11:05 Urhengulas

there are few things that should be better documented in the book

defmt currently does not support hosted environments like Linux, Windows, macOS, etc. this can be a bit of an issue when using / (host) testing libraries that use defmt

the practical consequence is that one should not use the logging macros in those environments. defmt's warn!, info!, debug!, trace! should not cause a problem because they are off by default. defmt::error! is enabled by default so if you have any of those -- note that defmt::panic! and defmt::assert! internally call defmt::error! -- then you should set DEFMT_LOG=off when compiling your crate for the hosted targets.

another option is to conditionally switch between defmt's logging macros and log's logging macros. for that you could use:

#[cfg(target_os = "none")] // embedded target
use defmt::{error, warn, info, debug, trace};

#[cfg(not(target_os = "none"))] // Linux, Windows, macOS, etc.
use log::{error, warn, info, debug, trace};

there's no way to disable defmt::println! with DEFMT_LOG so your option there is either the cfg approach or to never use println! in libraries.

if you would like to continue unconditionally using defmt::panic! and defmt::assert! -- or if you are dealing with a third party library that contains those -- then you should be able to set a defmt::panic_handler in your library to make cargo test work. something like this:

#[cfg(test)]
#[defmt::panic_handler]
fn panic() -> ! {
    core::panic!("panic via `defmt::panic!`")
}

japaric avatar May 31 '22 13:05 japaric

note that the defmt-error, etc. Cargo features are no longer used in defmt 0.3.0 because the filters are controlled via the environment variable DEFMT_LOG. see https://defmt.ferrous-systems.com/filtering.html

@haata let us know if implementing the suggestions in my previous comment let you fix the linker errors. from looking at the linker error you shared, it seems that you'll need the defmt::panic_handler to make cargo test work on Windows.

japaric avatar May 31 '22 13:05 japaric

Thanks! I'll give it a try this week.

haata avatar May 31 '22 15:05 haata

defmt currently does not support hosted environments like Linux, Windows, macOS, etc. this can be a bit of an issue when using / (host) testing libraries that use defmt

Is there any advice on how/whether we should implement defmt::Format in libraries? We've got a PR over in uuid https://github.com/uuid-rs/uuid/pull/639 that adds it, but have run into what I think is this issue in our CI with defmt::write!. We can work around it of course, but I've been wanting to keep our --all-features builds working on tier 1 platforms.

KodrAus avatar Oct 09 '22 03:10 KodrAus

I am running into link errors for a shared library that uses derive(Format), and no logging statements. The library contains structs that are used by my MCU, Linux, and Windows. It builds fine on Linux, but on Windows I get:

  = note: libshared-7e68f9b1edf48cc2.rlib(shared-7e68f9b1edf48cc2.xb1jn04cva8eh78.rcgu.o) : error LNK2019: unresolved external symbol _defmt_acquire referenced in function _ZN67_$LT$shared..message..FromHost$u20$as$u20$defmt..traits..Format$GT$6format17h0b99be05839c96deE
          libdefmt-021a50e4f73e2db0.rlib(defmt-021a50e4f73e2db0.defmt.ee3c75f0-cgu.9.rcgu.o) : error LNK2001: unresolved external symbol _defmt_acquire
          libshared-7e68f9b1edf48cc2.rlib(shared-7e68f9b1edf48cc2.xb1jn04cva8eh78.rcgu.o) : error LNK2019: unresolved external symbol _defmt_release referenced in function _ZN67_$LT$shared..message..FromHost$u20$as$u20$defmt..traits..Format$GT$6format17h0b99be05839c96deE
          libdefmt-021a50e4f73e2db0.rlib(defmt-021a50e4f73e2db0.defmt.ee3c75f0-cgu.9.rcgu.o) : error LNK2001: unresolved external symbol _defmt_release
          libshared-7e68f9b1edf48cc2.rlib(shared-7e68f9b1edf48cc2.xb1jn04cva8eh78.rcgu.o) : error LNK2019: unresolved external symbol _defmt_panic referenced in function _ZN67_$LT$shared..message..FromHost$u20$as$u20$defmt..traits..Format$GT$6format17h0b99be05839c96deE
          libdefmt-021a50e4f73e2db0.rlib(defmt-021a50e4f73e2db0.defmt.ee3c75f0-cgu.9.rcgu.o) : error LNK2019: unresolved external symbol _defmt_flush referenced in function _ZN5defmt5flush17h6023f786f51632fdE
          libdefmt-021a50e4f73e2db0.rlib(defmt-021a50e4f73e2db0.defmt.ee3c75f0-cgu.3.rcgu.o) : error LNK2019: unresolved external symbol _defmt_write referenced in function _ZN5defmt6export3f3217h160241a35c1e8599E
          libdefmt-021a50e4f73e2db0.rlib(defmt-021a50e4f73e2db0.defmt.ee3c75f0-cgu.6.rcgu.o) : error LNK2001: unresolved external symbol _defmt_write
          libdefmt-021a50e4f73e2db0.rlib(defmt-021a50e4f73e2db0.defmt.ee3c75f0-cgu.3.rcgu.o) : error LNK2019: unresolved external symbol _defmt_timestamp referenced in function _ZN5defmt6export6header17ha728642f8139efcdE
          C:\Users\james\Documents\GitHub\hw-gauge\daemon\target\debug\deps\hw_gauge_winsvc.exe : fatal error LNK1120: 6 unresolved externals

I'm not finding enough information online to know if this is an error on my part, or similar to reports above.

  • rustc 1.64.0 (a55dd71d5 2022-09-19)
  • defmt 0.3.2

jhillyerd avatar Oct 13 '22 04:10 jhillyerd

I just fell over this issue. I have a library where I derive defmt::Format for a type, but also want to run unit tests.

  = note: libdefmt-070f26dc2660ddc9.rlib(defmt-070f26dc2660ddc9.defmt.167e37fa-cgu.12.rcgu.o) : error LNK2019: unresolved external symbol _defmt_acquire referenced in function _ZN5defmt5flush17h2fc9ebaccd76d845E
          libdefmt-070f26dc2660ddc9.rlib(defmt-070f26dc2660ddc9.defmt.167e37fa-cgu.12.rcgu.o) : error LNK2019: unresolved external symbol _defmt_flush referenced in function _ZN5defmt5flush17h2fc9ebaccd76d845E
          libdefmt-070f26dc2660ddc9.rlib(defmt-070f26dc2660ddc9.defmt.167e37fa-cgu.12.rcgu.o) : error LNK2019: unresolved external symbol _defmt_release referenced in function _ZN5defmt5flush17h2fc9ebaccd76d845E
          libdefmt-070f26dc2660ddc9.rlib(defmt-070f26dc2660ddc9.defmt.167e37fa-cgu.1.rcgu.o) : error LNK2019: unresolved external symbol _defmt_write referenced in function _ZN5defmt6export3f3217hc6d5ce75e66caeb7E
          libdefmt-070f26dc2660ddc9.rlib(defmt-070f26dc2660ddc9.defmt.167e37fa-cgu.6.rcgu.o) : error LNK2001: unresolved external symbol _defmt_write
          libdefmt-070f26dc2660ddc9.rlib(defmt-070f26dc2660ddc9.defmt.167e37fa-cgu.1.rcgu.o) : error LNK2019: unresolved external symbol _defmt_timestamp referenced in function _ZN5defmt6export6header17he32352bb0799f419E
          C:\Users\msn\Documents\GitHub\Neotron-BMC\target\debug\deps\neotron_bmc_protocol-4f03f65c89f102c7.exe : fatal error LNK1120: 5 unresolved externals

thejpster avatar Dec 11 '22 11:12 thejpster

I'm also seeing this error, on macOS.

rustc --version rustc 1.71.0-nightly (87b1f891e 2023-04-29)

Cargo.toml:

defmt = "0.3.4"
defmt-rtt = "0.4"
 rust-lld: error: undefined symbol: _defmt_timestamp
          >>> referenced by mod.rs:98 (src/export/mod.rs:98)
          >>>               defmt-a21f348e44cd3538.defmt.ab223e99b9af9ea1-cgu.0.rcgu.o:(defmt::export::header::h0ee01d8d76e7d54e) in archive /Users//Desktop//target/thumbv6m-none-eabi/debug/deps/libdefmt-a21f348e44cd3538.rlib         

kpishere avatar May 22 '23 15:05 kpishere

Ah! I resolved this error by actually using the feature. Forcing the linking/loading of the timestamp function, I suppose.

defmt::timestamp!("Starting MyProgram...");

This un-blocks me but it would be better if a default non-implementation were there when it isn't used in the program?

kpishere avatar May 22 '23 15:05 kpishere

The default should be that timestamps are turned off: https://github.com/knurling-rs/defmt/blob/aceeaa07e09822cbf07efcd6b8fb41d09e8f0ee2/defmt/src/lib.rs#L361-L364

Does this error also happen to you on stable?

Urhengulas avatar May 22 '23 16:05 Urhengulas

@Urhengulas My project has no chance of building on stable. I'm doing no_std no-OS on RP2040 microcontroller, project will not build without nightly.

kpishere avatar May 22 '23 19:05 kpishere

I have similar errors. I am building a server that serializes and deserializes data according to a protocol defined in a crate which is shared between linux, the MCU, and windows. This protocol crate derives defmt::Format for a bunch of types.

I get these missing symbols:

  • _defmt_write
  • _defmt_timestamp
  • _defmt_acquire
  • _defmt_flush
  • _defmt_release

barafael avatar Oct 27 '23 14:10 barafael

@barafael: https://github.com/knurling-rs/defmt/issues/673#issuecomment-1142150012 is probably the answer you need.

thejpster avatar Oct 28 '23 15:10 thejpster

@thejpster thanks for the hint, this does not work though. I get the same error and these imports are unused - the library only uses defmt::Format, not info! etc.

barafael avatar Oct 28 '23 18:10 barafael

For reference, here's a repo to reproduce: https://github.com/barafael/defmt-on-windows-issue

barafael avatar Oct 28 '23 19:10 barafael

AFAIK you have to use cfg_attr to turn off anything defmt related. The whole job of the library is to hide strings in your debug info using linker magic and that is just incompatible with a using a Real Operating System.

See https://crates.io/crates/embedded-sdmmc for a library I wrote that uses log or defmt, using feature flags.

thejpster avatar Oct 28 '23 21:10 thejpster

Thanks @thejpster, that did the trick.

barafael avatar Oct 28 '23 22:10 barafael