如何让 Xcode 使用 clang 的替代版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23071024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make Xcode use an alternative version of clang?
提问by j b
How can I set up Xcode up to use the clang distributed by llvm.orginstead of the one shipped by Apple?
如何设置 Xcode 以使用llvm.org分发的clang而不是 Apple 提供的 clang ?
If I download the clang binariesand install to /usr/local
and then set my compiler to Other...-> /usr/local/bin/clang
in Xcode, then I get errors of the form:
如果我下载了clang 二进制文件并安装到/usr/local
然后将我的编译器设置为Other...->/usr/local/bin/clang
在 Xcode 中,那么我会收到以下形式的错误:
Unsupported compiler '/usr/local/bin/clang' selected for architecture 'x86_64'
Unable to determine concrete GCC compiler for file ...of type sourcecode.c.c.
为体系结构“x86_64”选择了不受支持的编译器“/usr/local/bin/clang”
无法确定文件...类型 sourcecode.cc 的具体 GCC 编译器
UPDATE:I've found a way to make this work by symlinking /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr
to the clang+llvm-3.4-x86_64-apple-darwin10.9
root directory, but it would be far better it there was a way to set an alternative compiler for Xcode without modifying the Xcode directory tree.
更新:我找到了一种通过符号链接/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr
到clang+llvm-3.4-x86_64-apple-darwin10.9
根目录来完成这项工作的方法,但如果有一种方法可以在不修改 Xcode 目录树的情况下为 Xcode 设置替代编译器,那就更好了。
回答by hagi
The easiest option (also the easiest to reverse) is to add the CC
build flag. Go to your project's or target's build settings, and choose Editor--> Add Build Setting--> Add User-Defined Settingfrom the menu.
最简单的选项(也是最容易逆转的)是添加CC
构建标志。转到您的项目或目标的构建设置,然后从菜单中选择编辑器-->添加构建设置-->添加用户定义的设置。
Name the flag CC
, and set the value to the path to your Clang binary.
命名 flag CC
,并将值设置为 Clang 二进制文件的路径。
You can choose whether you want to use this compiler for all builds, or just debug builds (this makes sense when you use experimental and/or self-modified versions of Clang, for instance).
您可以选择是要将此编译器用于所有构建,还是仅调试构建(例如,当您使用 Clang 的实验性和/或自修改版本时,这很有意义)。
Note that for features like autocompletion, symbolic search etc., Xcode does not use the Clang binary but other LLVM libraries distributed with Xcode. I've described how to replace those in another post(which is more or less the same approach that you took). By only changing the Clang version, you may get different warnings after compiling than those shown in the editor while typing.
请注意,对于自动完成、符号搜索等功能,Xcode 不使用 Clang 二进制文件,而是使用 Xcode 分发的其他 LLVM 库。我已经在另一篇文章中描述了如何替换它们(这或多或少与您采用的方法相同)。通过仅更改 Clang 版本,编译后您可能会收到与键入时编辑器中显示的警告不同的警告。
回答by MrMobster
Its an old question, but since I wanted to install more recent clang/libc++ to use with Xcode 9+, here is my solution in hope it would be useful for someone.
这是一个老问题,但由于我想安装更新的 clang/libc++ 以与 Xcode 9+ 一起使用,这是我的解决方案,希望它对某人有用。
- If you are building from source, make sure to pass
-DLLVM_CREATE_XCODE_TOOLCHAIN=ON
to cmake, afterwards run make install-xcode-toolchain. If you install with homebrew, use thebrew install --with-toolchain llvm
- Copy the toolchain folder from wherewher its installed (/usr/local/Toolchains probably) to /Library/Developer/Toolchains
- Restart Xcode, you will see a Toolchains option in the Xcode menu.
- 如果您是从源代码构建,请确保传递
-DLLVM_CREATE_XCODE_TOOLCHAIN=ON
给 cmake,然后运行 make install-xcode-toolchain。如果您使用自制软件安装,请使用brew install --with-toolchain llvm
- 将工具链文件夹从其安装位置(可能是 /usr/local/Toolchains)复制到 /Library/Developer/Toolchains
- 重新启动 Xcode,您将在 Xcode 菜单中看到一个 Toolchains 选项。
A particular issue I run into is that clang complains about "can't use -o option with multiple output files". This was because at the time of writing this Apple's clang has a new option that was missing in trunk. To fix this, disable the Index-While-Building
function in your project settings.
我遇到的一个特殊问题是 clang 抱怨“不能对多个输出文件使用 -o 选项”。这是因为在撰写本文时,Apple 的 clang 有一个在后备箱中缺失的新选项。要解决此问题,请Index-While-Building
在项目设置中禁用该功能。