substrate学习-编写简单pallet的问题记录
问题:
添加自定义pallet到Substrate框架中,编译时遇到如下错误:
--- stderr Compiling pallet-simple-pallet v4.0.0-dev (/Users/johnny/Documents/work/BlockChain/Substrate/substrate-node-template/pallets/simple-pallet) error: duplicate lang item in crate `sp_io` (which `frame_support` depends on): `panic_impl`. | = note: the lang item is first defined in crate `std` (which `pallet_simple_pallet` depends on) = note: first definition in `std` loaded from /Users/johnny/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-unknown/lib/libstd-f14dd22f8336e78a.rlib = note: second definition in `sp_io` loaded from /Users/johnny/Documents/work/BlockChain/Substrate/substrate-node-template/target/debug/wbuild/node-template-runtime/target/wasm32-unknown-unknown/release/deps/libsp_io-b3c1e7f3cef4f3ba.rmeta error: duplicate lang item in crate `sp_io` (which `frame_support` depends on): `oom`. | = note: the lang item is first defined in crate `std` (which `pallet_simple_pallet` depends on) = note: first definition in `std` loaded from /Users/johnny/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-unknown/lib/libstd-f14dd22f8336e78a.rlib = note: second definition in `sp_io` loaded from /Users/johnny/Documents/work/BlockChain/Substrate/substrate-node-template/target/debug/wbuild/node-template-runtime/target/wasm32-unknown-unknown/release/deps/libsp_io-b3c1e7f3cef4f3ba.rmeta error: could not compile `pallet-simple-pallet` due to 2 previous errors warning: build failed, waiting for other jobs to finish... error: build failed
解决办法:
在pallet的lib.rs第一行添加以下:
#![cfg_attr(not(feature = "std"), no_std)]
此句的意思是:如果没有启动这个feature就启动no_std属性,这个属性会阻止std引入作用域。
再重新编译。