学习rust要记住 编译器永远是对的
error: linking with `cc` failed: exit code: 1
|
= note: "cc" […]
= note: ld: entry point (_main) undefined. for architecture x86_64
clang: error: linker command failed with exit code 1 […]
这个错误消息告诉我们,链接器不能找到默认的入口点函数,它被命名为main——出于一些原因,macOS的所有函数名都被加以下划线_前缀。要设置入口点函数到_start,我们传送链接器参数-e: 这些操作都不用
很简单的操作呢
xcode-select --install解决
brew install cmake
统一所有的编译命令
我们的裸机程序已经可以在多个平台上编译,但对每个平台,我们不得不记忆和使用不同的编译命令。为了避免这么做,我们创建.cargo/config文件,为每个平台填写对应的命令:
# in .cargo/config
[target.'cfg(target_os = "linux")']
rustflags = ["-C", "link-arg=-nostartfiles"]
[target.'cfg(target_os = "windows")']
rustflags = ["-C", "link-args=/ENTRY:_start /SUBSYSTEM:console"]
[target.'cfg(target_os = "macos")']
rustflags = ["-C", "link-args=-e __start -static -nostartfiles"]
这里,rustflags参数包含的内容,将被自动添加到每次rustc调用中。我们可以在官方文档[3]中找到更多关于.cargo/config文件的说明。
做完这一步后,我们使用简单的一行指令——
cargo build