Skip to content

Instantly share code, notes, and snippets.

@thelibrarian
Last active November 6, 2017 03:28
Show Gist options
  • Save thelibrarian/5520597 to your computer and use it in GitHub Desktop.
Save thelibrarian/5520597 to your computer and use it in GitHub Desktop.
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.

@rduplain
Copy link

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
Copy link
Author

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

@ekowcharles
Copy link

thanks so much for posting this

@cesarvarela
Copy link

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

@xsynaptic
Copy link

Thanks, that worked for me!

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