Search code examples
xcodegccheader-filesosx-mountain-lionxcrun

xcrun gcc cannot find header files


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;
}

Solution

  • 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".