Cargo test not working on #![no_std]

I've encountered than when running cargo test on an embedded program that uses #![no_std], the compiler throws the following error:

error[E0152]: duplicate lang item in crate `std` (which `test` depends on): `panic_impl`.
  |
  = note: the lang item is first defined in crate `panic_rtt_target` (which `my_project` depends on)
  = note: first definition in `panic_rtt_target` loaded from /mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libpanic_rtt_target-c44e75acf48af2f7.rlib
  = note: second definition in `std` loaded from /home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-c6192dd4c4d410ac.so, /home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-c6192dd4c4d410ac.rlib

From this message I understand that, for some reason, cargo test is trying to use the standard library, which results in two different ways of handling panics, one from panic_rtt_target the other one from the standard library. Because of this, it can't compile the program.

Is this what is happening? If so, why does cargo test try to use the standard library and how can I tell it not to?

Thanks in advance

cargo test will compile your program with --test which links libtest as test harness. libtest depends on libstd for everything from spawning threads to printing test results. You can either set test = false on your program in Cargo.toml and add a program to tests/ and specify harness = false for this test in Cargo.toml to tell cargo that it has to be compiled and executed as a regular program rather than with a test harness. In this case #[test] won't work. The other option is to use the custom test framework unstable feature. This will allow #[test] to work, but requires you to write an entire test harness yourself. Testing | Writing an OS in Rust is a good description on how to do this.

1 Like

Not sure what type of embedded application/platform you have, but I have used defmt-test (part of knurling) to unit-test test cortex-m microcontrollers and it works like a charm.

From the knurling documentation:

defmt-test is an embedded test harness that lets you write and run unit tests as if you were using the built-in #[test] attribute, but they'll run on your embedded target.

Of course, defmt-test also gives you an #[init] attribute for initialization functions needed to set up your peripherals etc.

1 Like

The error message says you are trying to link a crate called panic-rtt-target for the x86_64-unknown-linux-gnu target. That means you are not trying to build the test suite for the target device or anything really complex, you're just trying to run tests on the host. This is perfectly reasonable, and exactly what I would do in my projects.

You don't need the panic_impl that this contains at all, since you can just use the one provided by the standard library. You should be able to avoid the additional lang item by conditionally including panic-rtt-target:

#![no_std]

#[cfg(not(test))]
use panic_rtt_target as _;

This is unfortunately very error prone, since any transitive dependency that includes this crate will also cause the same error. But with any luck, you should be able to control the beast enough to just make the normal test harness work.

1 Like

Thanks, that's exactly the case, I wrote some math functions and some basic tests for them, but I don't need to test them in the target hardware.

I tried to leave the out panic_rtt_target as you suggested, but I got a new error:

error: linking with `cc` failed: exit status: 1

I'm not sure if this is one of the dependency errors you mentioned or something else.

Thanks, I will have a look!

Hard to say without more info. The error messages are usually more detailed than that.

You can also increase the Cargo verbosity with cargo -vvvvv test for some number of vs (I don't actually know how many gives what level of detail). But with enough verbosity, it will also print the command lines that it executes for each step, as well as any build script outputs.

Somewhere in this gob of information is a hint to why linking fails.

Sorry, I didn't include the entire error message:

error: linking with `cc` failed: exit status: 1
| 
= note: LC_ALL="C"
PATH="/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/videbar/.local/bin:/home/videbar/.cargo/bin:/home/videbar/.local/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin"
VSLANG="1033" "cc" "-m64" "/tmp/rustcZyC97D/symbols.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.1084j7oqpjemlgxw.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.113cov9uc8q7xxt3.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.14871lc28humdbyb.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.19kv7fr20ujprlqb.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.1b6cmtup1fhc4klr.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.1ce1bzob1bvww4gi.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.1gf26lapqgbswibr.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.1h7r85lakbww26k0.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.1legsdwntmyo9f6e.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.1rd9i2r96a1sdhv9.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.1yw51h0jhyrex2rj.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.278so8cigm2as628.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.2lfrorf9q6oxqs5h.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.2oi1o8ikabaymizs.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.2slwyzmo5gof9klv.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.2sp3zzwmmpqrvajn.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.3bura5xh9mh58ct7.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.3pmfmrbyzr7itimf.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.3rxyrwle1o73fmqa.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.3ugf9qf97hd90lyn.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.3zrp0pn53qx6i62h.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.4857bjray6y7vtn.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.49vdni77fdw9pk2q.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.4m3t69hxo5p5sisx.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.4swy5jgw346pmjfk.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.51tgtqttabt5yqtk.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.5438f1cwn9mn525e.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.54r0ey8vpwq1wojb.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.5c4nr6b4m5bwelax.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.5gslbfw6yt0or1fy.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.8si6148nehvc314.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.dzo19yjbq1hfmmd.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.r46065du1p1t056.rcgu.o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd.3goygjbki7w5xbos.rcgu.o"
"-Wl,--as-needed" "-L"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps" "-L"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/build/cortex-m-rt-56e81c9734cd7e1d/out"
"-L"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/build/nrf52833-hal-4467230d4932b1fa/out"
"-L"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/build/nrf52833-pac-040a44434b061822/out"
"-L"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib"
"-Wl,-Bstatic"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libtest-7791a6353d4a7929.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgetopts-7b17724e9ce90a72.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunicode_width-596f8ce9d30a755e.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_std-da04f86254de594f.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-89bc084783fdc439.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-8bee4b287d4367c1.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-d61707aed80694c0.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-d85366256f22345b.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-96069b86b8a8cae9.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-d19d53abf68dfa6c.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-787cbccd19d64ac6.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-b3837a36b830e0d0.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-e3deb0e7e3f04966.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-dabbb79c9815def4.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-305b01f34c9409f2.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-f833521df6074e73.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-9ac333113350d171.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-1c126114322d0eee.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-bad9164fdeeecf92.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-f9374b1e480fa681.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/liblsm303agr-0e307c5232689120.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/librtt_target-e76d1750a22bad71.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libufmt_write-1c8042e332236abe.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libmicrobit-e4ae13072731749c.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libmicrobit_common-433f382ebcf43364.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libtiny_led_matrix-34a79dd436177494.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libnrf52833_hal-a074b0167364dc4b.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libnrf_hal_common-22fb912d2c5255f5.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libnrf_usbd-715c6ebcd1bd9c33.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libusb_device-9543f1a7acdcf564.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libcritical_section-ce71477a8c8505e4.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libcritical_section-e9ef95e8e933c9bd.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libbare_metal-bb4e84deb23bd74d.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libfixed-991f5a86f286dfa5.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libtypenum-d40a559dcc4c7492.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libbytemuck-86ddde4110e746a7.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libhalf-7a1580b5c8e83321.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libaz-ef23e0bd8200d29d.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/librand_core-22717effa7cad347.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libcfg_if-e5e118dd7d121aaa.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libembedded_storage-f810ce9a73dbdc66.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libembedded_dma-7b85cf45c05ae578.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libstable_deref_trait-1d58cfd993a3e774.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libcast-105792a9cb3b7cb7.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libnrf52833_pac-d19958c7d146907e.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libcortex_m-34f4330adfaf702a.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libvolatile_register-113659e36e9a0cd2.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libvcell-d1bd15f248f10f6a.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libbare_metal-af00298f9688d733.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libembedded_hal-5ccf1c5d0c58c307.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libvoid-0d39243f9215f71c.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libnb-348b0f2ef5839870.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libnb-16066e82b765c4b5.rlib"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libcortex_m_rt-8630d4aaae7cc331.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-207f06e41d9603af.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-7e2768e66e984e85.rlib"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-957b4aff41f8cd46.rlib"
"-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc"
"-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L"
"/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib"
"-o"
"/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/my_project-cd75592edefd18bd"
"-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs"
= note: /usr/bin/ld:
/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/librtt_target-e76d1750a22bad71.rlib(rtt_target-e76d1750a22bad71.rtt_target.fb92bad5-cgu.8.rcgu.o):
in function `cortex_m::interrupt::enable':
/home/videbar/.cargo/registry/src/github.com-1ecc6299db9ec823/cortex-m-0.7.7/src/interrupt.rs:48:
undefined reference to `__cpsie' /usr/bin/ld:
/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/librtt_target-e76d1750a22bad71.rlib(rtt_target-e76d1750a22bad71.rtt_target.fb92bad5-cgu.8.rcgu.o):
in function `cortex_m::interrupt::disable':
/home/videbar/.cargo/registry/src/github.com-1ecc6299db9ec823/cortex-m-0.7.7/src/interrupt.rs:38:
undefined reference to `__cpsid' /usr/bin/ld:
/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/librtt_target-e76d1750a22bad71.rlib(rtt_target-e76d1750a22bad71.rtt_target.fb92bad5-cgu.12.rcgu.o):
in function `cortex_m::register::primask::read':
/home/videbar/.cargo/registry/src/github.com-1ecc6299db9ec823/cortex-m-0.7.7/src/register/primask.rs:29:
undefined reference to `__primask_r' /usr/bin/ld:
/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libmicrobit_common-433f382ebcf43364.rlib(microbit_common-433f382ebcf43364.microbit_common.4346d5bf-cgu.14.rcgu.o):
in function `cortex_m::register::primask::read':
/home/videbar/.cargo/registry/src/github.com-1ecc6299db9ec823/cortex-m-0.7.7/src/register/primask.rs:29:
undefined reference to `__primask_r' /usr/bin/ld:
/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libmicrobit_common-433f382ebcf43364.rlib(microbit_common-433f382ebcf43364.microbit_common.4346d5bf-cgu.9.rcgu.o):
in function `cortex_m::interrupt::enable':
/home/videbar/.cargo/registry/src/github.com-1ecc6299db9ec823/cortex-m-0.7.7/src/interrupt.rs:48:
undefined reference to `__cpsie' /usr/bin/ld:
/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libmicrobit_common-433f382ebcf43364.rlib(microbit_common-433f382ebcf43364.microbit_common.4346d5bf-cgu.9.rcgu.o):
in function `cortex_m::interrupt::disable':
/home/videbar/.cargo/registry/src/github.com-1ecc6299db9ec823/cortex-m-0.7.7/src/interrupt.rs:38:
undefined reference to `__cpsid' collect2: 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#cargorustc-link-libkindname)

error: could not compile `my_project` due to previous error

Caused by: process didn't exit successfully:
`CARGO=/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/cargo
CARGO_BIN_NAME=my_project CARGO_CRATE_NAME=my_project
CARGO_MANIFEST_DIR=/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project
CARGO_PKG_AUTHORS='' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE=''
CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=my_project CARGO_PKG_REPOSITORY=''
CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=0.1.0 CARGO_PKG_VERSION_MAJOR=0
CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE=''
CARGO_PRIMARY_PACKAGE=1
LD_LIBRARY_PATH='/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps:/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib:/home/videbar/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib'
rustc --crate-name my_project --edition=2021 src/main.rs --error-format=json
--json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=190
--emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 --test -C
metadata=cd75592edefd18bd -C extra-filename=-cd75592edefd18bd --out-dir
/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps -C
incremental=/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/incremental
-L
dependency=/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps
--extern
cortex_m=/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libcortex_m-34f4330adfaf702a.rlib
--extern
cortex_m_rt=/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libcortex_m_rt-8630d4aaae7cc331.rlib
--extern
lsm303agr=/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/liblsm303agr-0e307c5232689120.rlib
--extern
microbit=/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libmicrobit-e4ae13072731749c.rlib
--extern
panic_rtt_target=/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/libpanic_rtt_target-808d0e148bfee72d.rlib
--extern
rtt_target=/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/deps/librtt_target-e76d1750a22bad71.rlib
-L
/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/build/cortex-m-rt-56e81c9734cd7e1d/out
-L
/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/build/nrf52833-hal-4467230d4932b1fa/out
-L
/mnt/9ac6c9f8-df73-4b94-abbf-7465ff541761/code/rust/my_project/target/debug/build/nrf52833-pac-040a44434b061822/out`
(exit status: 1)

There are undefined symbols because you are building the cortex_m crate for an x86_64 target. You might be able to add more #[cfg(not(test))] annotations on things that pull in the cortex_m crate.

Like I said, it's very error prone. If you can't conditionally-compile these crates, then you will not be able to run your code on the host.

Thanks, I see what you mean by error prone, as I'd need to carefully filter out everything that depends on the hardware target for the tests.
I've been reading the other sources posted here and what I ended up doing was moving the code I was trying to test to lib.rs, and telling cargo to ignore main.rs when running tests by adding the following to Cargo.toml:

[[bin]]
name = "my_project" # This is the package name.
test = false

The tests can then run using the standard library, since it is in main.rs where the #![no_std] macro is called and all the conflicting dependencies are imported.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.