9

I installed the recent version of clang (to /usr/local/). How do I set up Xcode to use this and not the built-in version of clang?

4
  • This should be on StackOverflow as is about development tools
    – mmmmmm
    Jun 18, 2013 at 23:54
  • @Mark as for your own question in Meta, the question shouldn't be considered off-topic.
    – Thecafremo
    Jun 19, 2013 at 7:24
  • @Thecafremo - This is not about how to use the windows in Xcode which could be considered OSX use but about how to program with Xcode (also I think this has been answered in SO before)
    – mmmmmm
    Jun 19, 2013 at 9:16
  • @Mark He is asking for a way of configuring Xcode, not a way of developing with it.
    – Thecafremo
    Jun 19, 2013 at 14:05

3 Answers 3

1

Custom toolchain answer:

  1. See the structure of /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr because that's how you'll need to arrange your installation. Also we'll use the ToolchainInfo.plist file.

  2. Copy the default xctoolchain bundle from above, and paste it into ~/Library/Developer/Toolchains. In the ~/Library/Developer/Toolchains/my_toolchain.xctoolchain/ToolchainInfo.plist file, change the identifier to something else.

  3. In the usr folder, delete the bin folder. Go to /usr/local and create a alias of the bin folder. Move that alias to usr folder in the toolchain. Do the same for every other folder. You can also create one single alias to the parent folder of the installation and rename it to usr and move it to toolchain file.

  4. In ~/.bash_profile, add a line export TOOLCHAINS=org.llvm.13.0.0git or and replace the org.llvm.13.0.0git with your identifier.

  5. Open Xcode, Xcode menu > Toolchains > select your toolchain.

  6. Every time you build a project, remember to set Enable index-while-building functionality to NO or you might get a build error.

  7. For your custom Clang, you might need to set export SDKROOT="/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk" or corresponding to your OS in the ~/.bash_profile too.

0

This is a copy of my answer on StackOverflow: How can I force Xcode to use custom compiler?.

People say it is possible with custom toolchains. I didn't make a research on them because easier solution worked well for me:

It is also possible to run frontend plugins directly by setting appropriate "build settings" of Xcode. (Several ways to do this, you can set them on the command line for instance: xcodebuild build FOO=bla.) Here are a few build settings that I found useful to inject C flags:

OTHER_CFLAGS, OTHER_CPLUSPLUSFLAGS or to replace the compiler(s) and linker(s):

CC, CPLUSPLUS, LD, LDPLUSPLUS, LIBTOOL

The same approach works to control the "analyze" action: CLANG_ANALYZER_EXEC, CLANG_ANALYZER_OTHER_FLAGS

Disclaimer: some of those build settings are undocumented (afaik). Use at your own risk.

(Taken from [cfe-dev] Compile/refactor iOS Xcode projects)

For me it was enough to define the following User-Defined Settings in Build Settings of Xcode projects:

CC=my-c-compiler

CXX=my-cxx-compiler

LIBTOOL=my-linker-for-static-libraries

1
  • You could have copied the formatting too. (click on edit and copy the answer)
    – Thinkr
    Apr 22, 2023 at 16:22
0

If you installed clang through the homebrew LLVM package, the following works for me with Xcode 14.3. It's at least an alternative to the other answers so far. I don't know whether this is supported by Apple nor whether it's advisable however.

Steps are:

  1. Ensure the path ~/Library/Developer/Toolchains exists:
mkdir -p ~/Library/Developer/Toolchains
  1. Symbolically link the toolchains from homebrew's LLVM installation:
ln -s /opt/homebrew/opt/llvm/Toolchains/* ~/Library/Developer/Toolchains 
  1. Restart Xcode. "Toolchains" shows up now (if it didn't before) under Xcode's Xcode menu and it has an Xcode option followed by one or more options that begin with "org.llvm".

Xcode menu with Toolchains

  1. Select Xcode -> Toolchains -> org.llvm.16.0.4 (or whichever release of LLVM you have installed through homebrew).
  2. If enabled, disable the "Enable Index-While-Building Functionality" build option (COMPILER_INDEX_STORE_ENABLE) from within projects' build settings for projects that are to be built with the new toolchain.

Steps 1 and 2 can alternatively use the /Library/Developer/Toolchains path, which is the same as above but without the ~ prefix. This presumably will provide system wide availability of the new toolchains. I have only confirmed that it also worked for me however. Those steps will probably need to be run through sudo then to have the privileges necessary for them.

2
  • Isn't this the same as apple.stackexchange.com/a/417985/237?
    – mmmmmm
    May 30, 2023 at 19:09
  • @mmmmmm A viewer's impression of the answer you linked (apple.stackexchange.com/a/417985/237) is that their steps 1, 2, 3, and 4 are different than steps 1 and 2 of my answer, and aren't specific to using homebrew. Additionally, I don't get from their answer, that copying "the default xctoolchain bundle from above... into ~/Library/Developer/Toolchains" may cause Toolchains menu option to appear. When I read that answer, I was like: I have no Toolchains menu option, so how do I even get that? So, my answer also makes clearer IMO how Toolchains appears. May 31, 2023 at 14:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .