许金龙
许金龙

Reputation: 91

How can I force Xcode to use a custom compiler?

I want to force Xcode to use a custom compiler ('clang-llvm' build from the src) so I can use the clang plugin. My Xcode version is 7.3.1.

Upvotes: 7

Views: 8120

Answers (5)

Cameron Lowell Palmer
Cameron Lowell Palmer

Reputation: 22225

Method 1: Change the User Defined settings

Under the project or target Build Settings add the User Defined settings for

CC=/path/to/cc
CXX=/path/to/c++

This is useful if you have a single compiler or linker you want to call, or if you want to call out to a trampoline that decides what to call on the fly.

User Defined Settings

Method 2: Create a complete custom toolchain via plugin

Using Clang LLVM 1.0.xcplugin as a template (found in the Xcode.app plugins folder), you can modify the plist to point at your own alternative compiler and linker.

This OLLVM on iOS tutorial walks through it.

Upvotes: 3

Stanislav Pankevich
Stanislav Pankevich

Reputation: 11368

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

If you use CMake, the way to inject your compiler automatically is to use

set_target_properties(your-target PROPERTIES XCODE_ATTRIBUTE_CC "${YOUR_CC}")
set_target_properties(your-target PROPERTIES XCODE_ATTRIBUTE_CXX "${YOUR_CXX}")

Upvotes: 11

Ken Thomases
Ken Thomases

Reputation: 90521

There's a somewhat obscure feature of Xcode where it supports "alternative toolchains". For example, Swift.org provides installable toolchains for Swift built from current sources.

Unfortunately, while Apple's documentation describes how to install and use such alternative toolchains, it doesn't describe how to create them. There are scripts in the Swift source base which build a toolchain and you can look at them to figure out how it's done. They are in https://github.com/apple/swift/tree/master/utils. Start at build-toolchain, which calls build-script and go from there.

Upvotes: 3

AlexDenisov
AlexDenisov

Reputation: 4117

Couple of years ago I've written an article that addresses exactly the problem you describe: Creating and using Clang plugin with Xcode

To enable custom clang you need to actually patch internals of Xcode.app itself, it is technically doable but:

  1. it will break when you update Xcode
  2. it will work correctly on your machine
  3. the version of a plugin and your compiler should match, i.e. they should be compiled using the same tree

So in general it doesn't really scale, so be careful :)

Upvotes: 3

Ketan Parmar
Ketan Parmar

Reputation: 27428

From project setting go to build setting with target selected. then select All beside the Basic from the top bar. then under build option you can see the compiler option.

Refer below screenshot,

enter image description here

Update :

I think you should refer Using C and C++ in an iOS App with Objective-C++ and this tutorial.

Upvotes: 1

Related Questions