Windows下用Rust为Linux生成.so文件遇问题求助
解决Rust编译生成Linux .so文件的问题
问题说明
我用Rust写了个跨平台库,打算在.NET6里通过P/Invoke调用,已经成功生成Windows的.dll文件。现在要在VSCode里编译Linux平台的.so文件,做了这些操作:
- 用rustup装了目标平台:
rustup target add x86_64-unknown-linux-musl - 执行编译命令没报错:
cargo build --target x86_64-unknown-linux-musl - 但在debug目录里只找到
.d和rlib文件,没看到想要的.so - 当前Cargo.toml的lib配置:
[lib] name="gx_hid" crate-type = ["cdylib", "lib"]
解决方法
- 找对产物路径:用
x86_64-unknown-linux-musl目标编译时,产物不在项目根目录的target/debug,而是在target/x86_64-unknown-linux-musl/debug/文件夹里,去这里找.so文件。 - 可选:编译发布版本:如果需要优化后的.so,加
--release参数编译:
产物会在cargo build --target x86_64-unknown-linux-musl --releasetarget/x86_64-unknown-linux-musl/release/目录下。 - 清理缓存重编译:如果配置没问题还是没生成,先清缓存再重新编译:
cargo clean cargo build --target x86_64-unknown-linux-musl - 检查工具链是否完整:确认musl目标平台是否正确安装,执行命令:
要是rustup target list --installedx86_64-unknown-linux-musl不在列表里,重新安装:rustup target add x86_64-unknown-linux-musl
内容的提问来源于stack exchange,提问作者Matteo Beretta
相关产品推荐
相关产品推荐

