我在使用 Rust 构建项目时遇到问题,我的 lib.rs 只有这个
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::{env, near_bindgen};
near_sdk::setup_alloc!();
#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
pub struct Counter {
// See more data types at https://doc.rust-lang.org/book/ch03-02-data-types.html
val: i8, // i8 is signed. unsigned integers are also available: u8, u16, u32, u64, u128
}
#[near_bindgen]
impl Counter {
pub fn get_num(&self) -> i8 {
return self.val;
}
self.val += 1;
let log_message = format!("Increased number to {}", self.val);
env::log(log_message.as_bytes());
after_counter_change();
}
pub fn decrement(&mut self) {
self.val -= 1;
let log_message = format!("Decreased number to {}", self.val);
env::log(log_message.as_bytes());
after_counter_change();
}
pub fn reset(&mut self) {
self.val = 0;
// Another way to log is to cast a string into bytes, hence "b" below:
env::log(b"Reset counter to zero");
}
}
fn after_counter_change() {
// show helpful warning that i8 (8-bit signed integer) will overflow above 127 or below -128
env::log("Make sure you don't overflow, my friend.".as_bytes());
}
当我运行 RUSTFLAGS='-C link-arg=-s' Cargo build --target wasm32-unknown-unknown --release 时,我收到此错误:
错误:未找到链接器
| = 注意:没有这样的文件或目录 (操作系统错误2)cc
错误:由于之前的错误
,无法编译proc-macro2
如果我将 proc-macro2 添加到依赖项中,错误将更改为无法编译...“其他依赖项”,并且当我尝试解决它时,它会不断添加新的依赖项,将它们添加到 Cargo.toml
[package]
name = "contract"
version = "0.1.0"
authors = ["Near Inc <[email protected]>"]
edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
near-sdk = "=4.0.0-pre.4"
[profile.release]
codegen-units=1
opt-level = "z"
lto = true
debug = false
panic = "abort"
overflow-checks = true
有什么帮助吗?
不需要,您只需要安装:
sudo apt install build-essential
编辑(添加更多信息):
Linux Rust 安装程序不会检查编译器工具链,但似乎假设您已经安装了 C 链接器!最好的解决方案是安装经过验证的 gcc 工具链。
我的 WSL ubuntu 出了问题,我的 Cargo 版本与 Windows 版本不同,能够使用 Windows 构建它
对于 Rust 和链接器的新手来说,最简单的解决方案就是运行
sudo apt install build-essential
稍后您可以了解不同的链接器,如何安装它们并将它们与货物一起使用。