Cargo flags `-zbuild-std=core,alloc` and rustc flag `--crate-type=staticlib` incompatible?

I would like to specify crate type on the command line because I need to use the crate both as a c-lib and a rust-lib. However, I also cross-compile and I want to compile core/alloc.

How can I do this?

I get the following compilation error:

cargo rustc -Zbuild-std=core,alloc -Zbuild-std-features=optimize_for_size --target=thumbv7em-none-eabi -- --crate-type=staticlib
   Compiling foo v0.1.0 (/Users/niklas/projects/niklas/test-crate-type/foo)
error: crate `core` required to be available in rlib format, but was not found in this form

error: crate `compiler_builtins` required to be available in rlib format, but was not found in this form

error: could not compile `foo` (lib) due to 2 previous errors

My test-crate only contains:

#![no_std]

use core::panic::PanicInfo;

pub fn add(left: u64, right: u64) -> u64 {
    left + right
}

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {}
}

Using crate-type = ["staticlib"] in the toml-file works fine.

AFAIK, your target platform needs to have the rust-src component present for Cargo to be able to build the standard library from source. According to rustup, the rust-src component is not available for thumbv7em-none-eabi.

I forgot to write that adding

[lib]
crate-type = ["staticlib"]

and compiling with

cargo rustc -Zbuild-std=core,alloc --target=thumbv7em-none-eabi
   Compiling foo v0.1.0 (/Users/niklas/projects/niklas/test-crate-type/foo)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.15s

works fine.

Found my mistake. I had an additional -- which was wrong.

This is the correct invocation:

cargo rustc -Zbuild-std=core,alloc --target=thumbv7em-none-eabi --crate-type=staticlib

The last (third) answer here is correct: rust - Is it possible to override the crate-type specified in Cargo.toml from the command line when calling cargo build? - Stack Overflow