MATLAB Answers


Can I use Xcode 4.2 to 4.6 as my C or C++ compiler in MATLAB? (Updated: 2013/02/04)

Asked by MathWorks Support Team on 18 Apr 2013
Latest activity Commented on by Walter Roberson
on 21 Jan 2016

I want to use one of the later versions of Xcode as my C compiler in MATLAB R2012b. Are Xcode versions 4.2-4.6 compatible with MATLAB 8.0 (R2012b)?


on 17 Feb 2014

This may or may not work for you, but using Xcode 5.0.2, I went to /Applications/ and (using sudo vi did a global change and replace from 10.7 to 10.9 and everything worked fine.

For all who want to use Xcode 5.0 on OS 10.9 see the following link for additional steps to the process:

on 6 Mar 2014

-Jeff Your fix works for me, thanks! I am using Xcode 5.0.2 on OSX 10.9

2 Answers

Answer by MathWorks Support Team on 18 Oct 2013
 Accepted answer

 If you have downloaded patches from this article before Februari 4, 2013, and you are still experiencing issues, please re-download and reapply the patches below, as they have been updated with additional fixes.  Note that the "Date Modified" values shown in this article may be different, due to minor edits that do not affect the patches.
 Update History:
 2013/02/04: added Xcode 4.6 support.
 2012/10/17: added Xcode 4.5 support and Simulink patch link.
 2012/09/13: added R2012b support.
 2012/09/05: added patch for Mac OS X 10.8.

* Scope of this solution *

This solution concerns the Mac platform only, with the following software versions only:

MATLAB versions:

- R2011a

- R2011b

- R2012a

- R2012b


Xcode versions:

- 4.6

- 4.5

- 4.4

- 4.3

- 4.2

- 4.1


Mac OS X versions:

10.6 (Snow Leopard), for Xcode versions 4.1 and 4.2 only

10.7 (Lion), for Xcode versions 4.1 to 4.6 only

10.8 (Mountain Lion), for Xcode versions 4.4 to 4.6 only

Do not apply the steps described in this solution to any other combination of MathWorks and Apple software.

For MATLAB releases before R2011a, use an older compatible version of Xcode as documented in the page “Supported and Compatible Compilers - Previous Releases”:

 ---For Simulink Users---
 For Simulink users utilizing Accelerator, Rapid Accelerator and Code Generation functionality, refer to this article to download additional Simulink patches AFTER applying the MATLAB patches below:

* Description of the issue *

If you use one of the above listed MATLAB releases with one of the above Xcode releases, you may see an error message like the following when using MEX, or attempting to use any other feature reliant on the presence of a C/C++ compiler:


 /Applications/ line 305: gcc-4.2: command not found

This is happening because MATLAB is expecting GCC 4.2 to be present. This compiler was distributed with Xcode 4.0 and 4.1, but is no longer supplied as of Xcode 4.2.

However, Xcode 4.2 and later include a similar compiler (GCC 4.2 front-end to LLVM) that MATLAB can be instructed to use instead. Applying the patch attached to this solution will instruct MATLAB to use the compiler supplied with Xcode 4.2 or later.


 /Applications/ error: stdlib.h: No such file or directory

This is happening because the SDKROOT directory for Xcode 4.2 and earlier are:


but Apple changed this directory to


in its Xcode versions 4.3 and higher. Applying the patch sets the correct SDK root depending on the Xcode compiler present on the computer.

You may need administrative permissions on your Mac to complete these steps.

* Before applying the patch *

1. Ensure your Xcode is installed in the default directory and has the "" default name. For Xcode 4.3 and later, this is:


For Xcode 4.1 and 4.2, this is:


2. If using Xcode 4.3 and later, ensure that the Xcode "Command Line Tools" are installed. To do this:

Since Xcode 4.3, the command line tools needed by MATLAB must be downloaded after the installation of Xcode itself. This does not happen by default. In Xcode, go to Preferences -> Downloads -> Components, and check “Check for and install updates automatically” and install the “Command Line Tools”.

3. After installing the "Command Line Tools" above, confirm that the following command (in the MATLAB command window)

!xcodebuild -version

returns the proper version of your Xcode. If this command does not return the version of Xcode, your Xcode was either installed incorrectly or installed in a non-default location.

4. If you have multiple versions of Xcode, make certain that the version you want to use for MATLAB has the default "" name, is in the default installation directory, and that this version has the "Command Line Tools" installed, i.e., the Xcode version must be the same for all three steps above. Note that only one version of "Command Line Tools" can be installed on a machine at any given time (installing it from different versions will overwrite the previous version).

* To apply the patch *

1. Download the appropriate patch for your MAC OS X version, attached to this Solution:




These instructions assume you download it to the default location Safari uses, namely the “Downloads” folder of your home folder.

2. Start MATLAB, if it is not already running.

3. In the MATLAB Command Window, type:

>> cd(matlabroot)
>> !unzip -o ~/Downloads/optsPatch10.8.patch


>> cd(matlabroot)
>> !unzip -o ~/Downloads/optsPatch10.7.patch


>> cd(matlabroot)
>> !unzip -o ~/Downloads/optsPatch10.6.patch

depending on the patch that was downloaded. Administrator privileges may be required for this step.

4. Set your compiler configuration by running:

>> mex -setup

5. If you use MATLAB Compiler, run the command:

>> mbuild -setup

MEX, MATLAB Compiler, and other MathWorks products should now be configured to use the compiler provided with Xcode 4.2 or later.

* Troubleshooting *

If, after installing the patch, you get an error such as this:

 /Applications/ line 305: llvm-gcc-4.2: command not found

you should check to make sure that a supported version of Xcode is installed on your machine. Launch Xcode, either from the Dock or from your Applications folder.

If Xcode launches, click "About Xcode" to make sure the version is within 4.1 to 4.6:

If you cannot find Xcode, cannot launch it, or the version is not Xcode 4.1, 4.2, 4.3, 4.4, 4.5 or 4.6 you should re-install Xcode and repeat steps 4 and 5 above.

If Xcode launches, but issues remain, confirm that all the prerequisites listed in the "Before applying the patch" section above are followed.

If after the above steps, Xcode is installed correctly and works as intended, but it still cannot be used in MATLAB, try applying an older version of the patch below:


(For MAC OS X 6, 7, or 8 with Xcode 4.2 or higher)

 WARNING: This patch does not set the SDKROOT property and may fail depending on your code to be compiled.  Only try this patch as a last resort, if the other OS specific patch fails and Xcode is tested to be working properly outside of MATLAB.

* Note *

The patches attached to this solution can be used with any combination of MATLAB release / Xcode version as described in the scope of this solution (see above).


on 16 Sep 2014

Is there a patch for mac 10.9?

Any updates on the newer versions? I have the following specs: OSX Yosmite 10.10.5 MATLAB R2015b (8.6.0) XCode 6.4

Answer by Nando Hoffman
on 23 Dec 2013

Hello! Will there be a solution patch for OSX 10.9 and Xcode 4.6? Thanks :)


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!