Xcode xcrun gcc找不到头文件

Xcode xcrun gcc找不到头文件,xcode,gcc,header-files,osx-mountain-lion,xcrun,Xcode,Gcc,Header Files,Osx Mountain Lion,Xcrun,我想在OSX mountain lion中编译一个c文件。在Xcode 4.4中,首选项->下载我可以安装命令行工具来实现这一点。但是,在该窗格中,它建议我可以改用xcrun: 在安装之前,请注意,可以从终端内部使用XCRUN工具启动编译器和嵌入Xcode应用程序的其他工具。使用XCODE-SELECT工具定义激活的XCODE版本。键入man xcrun from in Terminal以了解更多信息 我很乐意这样做,但我很难找到stdio.h $ xcrun gcc hello.c hello

我想在OSX mountain lion中编译一个c文件。在Xcode 4.4中,首选项->下载我可以安装命令行工具来实现这一点。但是,在该窗格中,它建议我可以改用xcrun:

在安装之前,请注意,可以从终端内部使用XCRUN工具启动编译器和嵌入Xcode应用程序的其他工具。使用XCODE-SELECT工具定义激活的XCODE版本。键入man xcrun from in Terminal以了解更多信息

我很乐意这样做,但我很难找到stdio.h

$ xcrun gcc hello.c
hello.c:1:19: error: stdio.h: No such file or directory
在通过App Store标准安装Xcode后,我的系统中肯定有此文件: /Applications/Xcode.app/Contents/Developer//Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/stdio.h

我尝试指定SDK,但出现了相同的错误:

$ xcrun -sdk /Applications/Xcode.app/Contents/Developer//Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk gcc hello.c
一些谷歌搜索让我尝试在上面的命令之前运行xcode select,但没有成功。我只安装了一个版本的Xcode

$ sudo xcode-select -switch /Applications/Xcode.app
如何让它找到标题

最终我放弃并安装了命令行工具,但如果知道如何使用内置的Xcode工具,那就太好了

注意:以下是我试图编译的文件:

#include <stdio.h>

int main() {
    printf("hello world!\n");
    return 0;
}

您必须为编译器指定非标准sysroot

使用新标准clang/llvm可以实现以下目的:

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

如果您想坚持使用gcc,只需将clang替换为gcc。

这可能没有任何直接帮助,但您可以为常用的xcrun命令设置别名,以便在其他进程调用gcc、make、gdb、git等时使用Xcode版本:

    alias gcc='xcrun gcc'
如果愿意,可以将别名放入.bashrc文件中,使其保持不变,并根据需要从.bash_配置文件中获取别名


所有这些的优点是,您不需要安装Xcode命令行工具,还可以避免使用包管理器,节省空间,降低复杂性,并利用Xcode自动为您更新这些工具。

使用xcrun-sysroot对我使用10.8不起作用。 查看clang文档,我发现在本例中可以使用-isysroot

使用:

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

对我来说,创建以下别名很有用:

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'

是的,这是一个非常有用的观点,尽管这不是问题的答案。
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'