Related Questions

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

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 HONGYU on 16 Sep 2014 at 9:19

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

4 Comments

Jeff on 17 Feb 2014

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

Logan Huba on 18 Feb 2014

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

http://www.mathworks.com/matlabcentral/answers/103258#answer_112685

Xu on 6 Mar 2014

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

MathWorks Support Team

Products

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

AND

Xcode versions:

- 4.6

- 4.5

- 4.4

- 4.3

- 4.2

- 4.1

AND

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”:

http://www.mathworks.com/support/sysreq/previous_releases.html

 
 ---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:
<http://www.mathworks.com/matlabcentral/answers/96437>

* 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:

1.

 
 /Applications/MATLAB_R2011b.app/bin/mex: 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.

2.

 
 /Applications/MATLAB_R2012a.app/extern/include/matrix.h:852:20: error: stdlib.h: No such file or directory

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

/Developer/SDKs/MacOSX10.7.sdk/

but Apple changed this directory to

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/

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 "Xcode.app" default name. For Xcode 4.3 and later, this is:

/Applications/Xcode.app

For Xcode 4.1 and 4.2, this is:

/Developer/Applications/Xcode.app

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 "Xcode.app" 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:

optsPatch10.6.patch

optsPatch10.7.patch

optsPatch10.8.patch

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

or

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

or

>> 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/MATLAB_R2011b.app/bin/mex: 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:

Xcode4_OLD.patch

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

4 Comments

Jorik on 18 Nov 2013

Thanks for adding the link. The attachments are now shown at the bottom of the answer.

HONGYU on 16 Sep 2014 at 9:19

Is there a patch for mac 10.9?

MathWorks Support Team
Answer by Nando Hoffman on 23 Dec 2013

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

0 Comments

Nando Hoffman

Contact us