C 将裸机ARM的Rust代码正确编译为静态库,并将其编译为x86_64的二进制代码?
我想做的是: 我有一段我想要构建的代码C 将裸机ARM的Rust代码正确编译为静态库,并将其编译为x86_64的二进制代码?,c,rust,arm,embedded,C,Rust,Arm,Embedded,我想做的是: 我有一段我想要构建的代码 作为裸机ARM的静态库,因此它可以链接到现有的c程序,以及 作为x86_64的可执行文件,因此我可以开发、单元测试等 我可以同时做这两件事,但不幸的是,我每次都要更换Cargo.toml 梅因 使用foobar::foo::Bar; fn main(){ 让bar=bar::new(); 让我们做点什么吧; // ... } 图书馆 #![特征(核心要素)] #![cfg_attr(feature=“EMBEDDED”,无标准)] #[cfg(fe
- 作为裸机ARM的静态库,因此它可以链接到现有的c程序,以及
- 作为x86_64的可执行文件,因此我可以开发、单元测试等
使用foobar::foo::Bar;
fn main(){
让bar=bar::new();
让我们做点什么吧;
// ...
}
图书馆
#![特征(核心要素)]
#![cfg_attr(feature=“EMBEDDED”,无标准)]
#[cfg(feature=“EMBEDDED”)]
使用core::intrinsics;
#[cfg(feature=“EMBEDDED”)]
使用core::panic::PanicInfo;
#[cfg(feature=“EMBEDDED”)]
#[恐慌处理程序]
fn恐慌(_info:&PanicInfo)->!{
不安全的{intrinsics::abort()}
}
#[cfg(非(feature=“EMBEDDED”)]
酒店;
福斯
pub结构栏;
注入棒{
pub fn new()->Self{
Bar{}
}
pub fn do_something(&self)->u32{
42
}
}
#[没有损坏]
pub extern“C”fn从\u C\u prog()调用\u->u32{
让bar=bar::new();
做点什么
}
#[cfg(测试)]
模试验{
使用超级::*;
#[测试]
fn foo_测试(){
让bar=bar::new();
让我们做点什么吧;
断言(ret,42);
}
}
货舱
[package]
name = "foobar"
version = "0.1.0"
authors = ["..."]
edition = "2018"
[dependencies]
#[[bin]]
#name = "foobar"
#path = "src/main.rs"
#test = true
#bench = false
[lib]
name = "foobar"
path = "src/lib.rs"
crate-type = ["staticlib"] # Creates static lib
#crate-type = ["cdylib"] # Creates synamic lib
[features]
default = []
EMBEDDED = []
通过这个Cargo.toml,我可以通过运行Cargo将代码构建为静态库
cargo build --lib --release --features EMBEDDED --target thumbv7em-none-eabihf
但是当我试图用
cargo test --target x86_64-unknown-linux-gnu
我得到这个错误:
error[E0433]: failed to resolve: use of undeclared type or module `foobar`
--> src/main.rs:1:5
|
1 | use foobar::foo::Bar;
| ^^^^^^ use of undeclared type or module `foobar`
error[E0433]: failed to resolve: use of undeclared type or module `Bar`
--> src/main.rs:4:15
|
4 | let bar = Bar::new();
| ^^^ use of undeclared type or module `Bar`
error: aborting due to 2 previous errors
当启用这两个部分([[bin]]+[lib])时,我得到了相同的错误
因此,当我现在将Cargo.toml更改为
[[bin]]
name = "foobar"
path = "src/main.rs"
test = true
bench = false
#[lib]
#name = "foobar"
#path = "src/lib.rs"
#crate-type = ["staticlib"] # Creates static lib
##crate-type = ["cdylib"] # Creates synamic lib
我可以用
cargo test --target x86_64-unknown-linux-gnu
但如果你现在就跑
cargo build --lib --release --features EMBEDDED --target thumbv7em-none-eabihf
我得到了一个.rlib,这是显而易见的,因为该部分被注释掉了
所以,我现在的问题是:
我怎样才能做到这两件事,而不必每次更换Cargo.toml?我还没找到合适的方法
问候来自:
--清单路径
Cargo.toml文件的路径。默认情况下,Cargo在当前目录或任何父目录中搜索Cargo.toml文件
只需有两个Cargo.toml
文件,并使用--manifest path
指定清单文件。还可以对两个版本使用不同的--target dir
,以便同时生成这两个版本