Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make Xcode use an alternative version of clang?

How can I set up Xcode up to use the clang distributed by llvm.org instead of the one shipped by Apple?

If I download the clang binaries and install to /usr/local and then set my compiler to Other... -> /usr/local/bin/clang in Xcode, then I get errors of the form:

Unsupported compiler '/usr/local/bin/clang' selected for architecture 'x86_64'

Unable to determine concrete GCC compiler for file ...of type sourcecode.c.c.

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.

like image 642
j b Avatar asked Apr 14 '14 21:04

j b


People also ask

How do I change my Clang version?

Select the Configuration Properties > Advanced property page. Modify the LLVM Toolset Version property to your preferred version, and then choose OK to save your changes.

Does Xcode use gcc or Clang?

In Xcode, the LLVM compiler uses the Clang front end (a C-based languages project on LLVM.org) to parse source code and turn it into an interim format. Then the LLVM code generation layer (back end) turns that interim format into final machine code.

Does Mac use GCC or Clang?

Apple ships the clang/LLVM compiler with macOS. Clang is a "front-end" that can parse C , C++ and Objective-C down to something that LLVM (referred to as a "back-end") can compile. Clang/LLVM is located in /Applications/Xcode.

Does Apple use Clang?

Clang is also provided in all major BSD or GNU/Linux distributions as part of their respective packaging systems. From Xcode 4.2, Clang is the default compiler for Mac OS X.


2 Answers

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 Setting from the menu.

Adding custom build flags

Name the flag CC, and set the value to the path to your Clang binary.

Setting the <code>CC</code> flag

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).

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.

like image 187
hagi Avatar answered Oct 13 '22 00:10

hagi


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.

  1. 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 the brew install --with-toolchain llvm
  2. Copy the toolchain folder from wherewher its installed (/usr/local/Toolchains probably) to /Library/Developer/Toolchains
  3. Restart Xcode, you will see a Toolchains option in the Xcode menu.

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.

like image 24
MrMobster Avatar answered Oct 12 '22 23:10

MrMobster