Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to fix compile errors with the XCode command line tools on Mac OS X. Solves problems such as failing to find Framework header files (e.g. ruby.h).

The Problem

If you have installed the standalone Command Line Tools for XCode on your Mac (i.e. without having XCode.app installed), some of these tools can get a bit confused due to a couple of oversights on Apple's part in finalising the setup.

Note: all commands below will need to be run from an Administrator account, or by an account with appropriate permission in /etc/sudoers.

The Solution

1. Failing to Find Frameworks

Sometime when compiling against the preinstalled Frameworks (e.g. Ruby or Python), various tools will inexplicable fail to find header files that are quite clearly there. This is caused by the fact that no XCode has been selected for the command-line tools. Wait, I hear you cry, I don't have XCode installed! Indeed, but you nonetheless need to select one, and point it somewhere where the command line tools exist, like so

sudo xcode-select -switch /usr/bin

2. xcrun

Now you may come across another problem - xcrun fails with the following error:

xcrun: Error: failed to exec real xcrun. (No such file or directory)

The standalone command line tools installer seems to not install a valid xcrun binary, so we get this error. This fix is a bit of a messy hack, and involves making a dummy xcrun like so:

sudo mv /usr/bin/xcrun /usr/bin/xcrun-orig
sudo vim /usr/bin/xcrun

Enter the following into your dummy xcrun file:

#!/bin/sh
exec "$@"

Now make it executable:

sudo chmod 755 /usr/bin/xcrun

And all should be right with the world!

Credit Where Credit Is Due

I did not work this out myself - I just managed to eventually find answers to both of these problems by searching around.

XCode problem:

This homebrew issue, particularly this comment by @beerlington provided the answer.

xcrun problem:

This was solved in this StackOverflow question by the asker Lukas Grebe based on the accepted answer by Ned Deily. Improved based upon a comment below by @rduplain.

@draffauf

This comment has been minimized.

draffauf commented Oct 6, 2013

Thanks so much for posting this. This fixed my problem right away!

@thelibrarian

This comment has been minimized.

Owner

thelibrarian commented Oct 7, 2013

You're welcome - I'm glad it helped!

@ankopainting

This comment has been minimized.

ankopainting commented Oct 8, 2013

+1 thank you

@bitoiu

This comment has been minimized.

bitoiu commented Oct 13, 2013

Amazing, worked for me!

@rduplain

This comment has been minimized.

rduplain commented Oct 15, 2013

Use quoted arguments in your xcrun shell script:

#!/bin/bash
exec "$@"

You will have issues without the quotes such that arguments will not expand correctly. Without quotes, xcrun bash -c 'echo "space test"' prints an empty line; with quotes, it correctly prints space test. The addition of exec is nice to have, replacing the shell process with the provided arguments.

From man bash:

"$@" is equivalent to "$1" "$2" ...

@thelibrarian

This comment has been minimized.

Owner

thelibrarian commented Oct 16, 2013

@rudplain Thanks very much for that - it is a great improvement.

@ekowcharles

This comment has been minimized.

ekowcharles commented Feb 18, 2014

thanks so much for posting this

@cesarvarela

This comment has been minimized.

cesarvarela commented Apr 24, 2014

Worked beautifully in 10.8.5, thank you very much, sir!

@xsynaptic

This comment has been minimized.

xsynaptic commented Jan 8, 2015

Thanks, that worked for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment