我在 macOS 10.13.5 上学习 Rust 编程,我使用 Nix 来控制我的开发环境。
某些操作(例如包含 jsonwebtoken
库或安装 cargo-watch
模块)会导致构建需要似乎未安装的 macOS 框架。我收到此错误消息:
= note: ld: framework not found CoreServices
clang-5.0: error: linker command failed with exit code 1 (use -v to see invocation)
error: aborting due to previous error
error: failed to compile `cargo-watch v6.0.0`, intermediate artifacts can be found at `/var/folders/13/84dj8yr54_1c_pn0s8n7444h0000gn/T/cargo-install.lYPZaEduUBdu`
Caused by:
Could not compile `cargo-watch`.
这是失败的 clang 命令的缩写版本:
error: linking with `/nix/store/9j864incgjx7kqggbpisdi3nmssy4qm5-clang-wrapper-5.0.2/bin/cc` failed: exit code: 1
|
= note: "/nix/store/9j864incgjx7kqggbpisdi3nmssy4qm5-clang-wrapper-5.0.2/bin/cc" "-m64" "-L" ... "/nix/store/rfp87664xzhl6zv7dx5c1hixasqfxkp4-rustc-1.24.0/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-ba331b20e371c580.rlib" "-framework" "CoreServices" "-framework" "CoreServices" "-l" "System" "-l" "resolv" "-l" "pthread" "-l" "c" "-l" "m"
我发现要尝试的唯一一件事是将框架添加到我的 PATH,但是这个答案是错误的,或者 PATH 环境变量无法通过 cargo 一直到达我正在构建的地方有问题。
- Clang(LLVM) compile with frameworks
使用rust :1.24.0
- cargo :0.25.0
- json网络 token :0.4.0
- cargo-watch:
cargo install cargo-watch
的默认版本
我如何告诉 clang 到哪里寻找框架?它是否涉及改变我的工作环境,或者我是否需要考虑改变我想要安装的 crate 的构建过程?
更多信息
我发现了 clang -Xlinker -v
命令,输出很有趣:
@(#)PROGRAM:ld PROJECT:ld64-274.2
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em (tvOS)
Library search paths:
/nix/store/ql6xbmdplca4sjpk0pz647p7djzri03c-libc++-5.0.2/lib
/nix/store/rfp87664xzhl6zv7dx5c1hixasqfxkp4-rustc-1.24.0/lib
/nix/store/ql6xbmdplca4sjpk0pz647p7djzri03c-libc++-5.0.2/lib
/nix/store/rfp87664xzhl6zv7dx5c1hixasqfxkp4-rustc-1.24.0/lib
/nix/store/8ykfqv6jx9jvfhnc4cdygdzg0piy8253-Libsystem-osx-10.11.6/lib
/nix/store/4papfih2r9xlsl9m7hlisparij8k9zaq-clang-5.0.2-lib/lib
Framework search paths:
/nix/store/hc6d711vwlwnn9swmkdpi9nbswbqg6h0-CF-osx-10.10.5/Library/Frameworks
/nix/store/hc6d711vwlwnn9swmkdpi9nbswbqg6h0-CF-osx-10.10.5/Library/Frameworks
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang-5.0: error: linker command failed with exit code 1 (use -v to see invocation)
这似乎表明我的 Nix shell 中缺少某些东西,而不是操作系统甚至 clang
本身。
最佳答案
显然,Nix 都为标准 Apple 框架和沙盒环境提供了包,使得标准框架不可用。
我为这个解决方案发现的大部分内容来自 Use proper SDK and command-line tools on OS X 10.11然后检查 vim-plugins nix derivation .
第一步是实际安装我的项目需要的框架。它们都存在于 nixpkgs.darwin.apple_sdk.frameworks
中。
这样做可以使大部分链接正常工作,但是 _CFURLResourceIsReachable
在我的平台上是一个 undefined symbol 。我用更新的 NIX_LDFLAGS
变量解决了这个问题(如 vim-plugins nix 推导中所建议的那样)。我的项目的最终结果是这个 shell.nix
文件:
let
pkgs = import <stable> {};
frameworks = pkgs.darwin.apple_sdk.frameworks;
in pkgs.stdenv.mkDerivation {
name = "orizentic";
buildInputs = [ pkgs.rustc
pkgs.cargo
frameworks.Security
frameworks.CoreFoundation
frameworks.CoreServices
];
shellHook = ''
export PS1="[$name] \[$txtgrn\]\u@\h\[$txtwht\]:\[$bldpur\]\w \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty \[$bldylw\]\$aws_env\[$txtrst\]\$ "
export NIX_LDFLAGS="-F${frameworks.CoreFoundation}/Library/Frameworks -framework CoreFoundation $NIX_LDFLAGS";
'';
}
这给了我 cargo-watch
包(它依赖于 CoreServices
和 CoreFoundation
)。它还显然解决了 jsonwebtoken
对 Security
的依赖,尽管我还没有设法验证这一点。
关于macos - 如何在 Nix 环境中使 macOS 框架可用于 clang?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51161225/