質問

I want to compile a c file in OSX mountain lion. In Xcode 4.4, Preferences -> Downloads I can install command line tools to do that. However on that pane it advises me that I can use xcrun instead:

Before installing, note that from within Terminal you can use the XCRUN tool to launch compilers and other tools embedded within the Xcode application. Use the XCODE-SELECT tool to define which version of Xcode is active. Type "man xcrun" from within Terminal to find out more.

I'd be happy to do it that way, but I'm having trouble getting it to find stdio.h.

$ xcrun gcc hello.c
hello.c:1:19: error: stdio.h: No such file or directory

I definitely have this file on my system after a standard install of Xcode via the App Store: /Applications/Xcode.app/Contents/Developer//Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/stdio.h

I tried specifying the SDK but got the same error:

$ xcrun -sdk /Applications/Xcode.app/Contents/Developer//Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk gcc hello.c

Some googling led me to try running xcode-select before the commands above, but no luck. I only have the one version of Xcode installed anyway.

$ sudo xcode-select -switch /Applications/Xcode.app

How can I get it to find the headers?

Ultimately I gave up and installed command line tools, but it would be nice to know how to do it with the built-in Xcode tools.

Note: here is the file I was trying to compile:

#include <stdio.h>

int main() {
    printf("hello world!\n");
    return 0;
}
役に立ちましたか?

解決

You will have to specify the non-standard sysroot for the compiler.

Using clang/llvm (which is the new standard) this would do the trick:

xcrun clang --sysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/ -o hello hello.c 

If you want to stick with gcc just replace "clang" with "gcc".

他のヒント

Probably not of any direct help with this, but you can set up aliases to commonly used xcrun commands so that when other processes call gcc, make, gdb, git and so on that the Xcode versions get used:

    alias gcc='xcrun gcc'

You can put the alias into the .bashrc file if you like so that it persists, and source from .bash_profile as necessary.

The advantage of all this is that you don't need to install the Xcode Command Line Tools and can also avoid using package managers, saving space, reducing complexity and taking advantage of Xcode automatically updating these tools for you.

Using xcrun --sysroot was not working for me using 10.8. Looking in to the clang documentation I found that -isysroot is the option to use in this case.

using:

xcrun gcc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk

or:

xcrun clang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk

For me it was useful to create the following aliases:

alias xcrungcc='xcrun gcc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk'
alias xcrunclang='xcrun clang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk'
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top