xcode xcrun gcc 找不到头文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11838993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
xcrun gcc cannot find header files
提问by Stephen Karger
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:
我想在 OSX 山狮中编译 ac 文件。在 Xcode 4.4 中,首选项 -> 下载我可以安装命令行工具来做到这一点。但是,在该窗格中,它建议我可以改用 xcrun:
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.
在安装之前,请注意,您可以在终端内使用 XCRUN 工具启动编译器和其他嵌入 Xcode 应用程序的工具。使用 XCODE-SELECT 工具来定义哪个版本的 Xcode 处于活动状态。在终端中输入“man xcrun”以了解更多信息。
I'd be happy to do it that way, but I'm having trouble getting it to find stdio.h.
我很乐意这样做,但我很难让它找到 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
通过 App Store 标准安装 Xcode 后,我的系统上肯定有这个文件:/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:
我尝试指定 SDK 但得到了同样的错误:
$ 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.
一些谷歌搜索使我尝试在执行上述命令之前运行 xcode-select,但没有运气。无论如何,我只安装了一个版本的 Xcode。
$ 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.
最终我放弃并安装了命令行工具,但如果知道如何使用内置的 Xcode 工具来完成它会很好。
Note: here is the file I was trying to compile:
注意:这是我试图编译的文件:
#include <stdio.h>
int main() {
printf("hello world!\n");
return 0;
}
回答by pbrc
You will have to specify the non-standard sysroot for the compiler.
您必须为编译器指定非标准的 sysroot。
Using clang/llvm (which is the new standard) this would do the trick:
使用 clang/llvm(这是新标准)可以解决这个问题:
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".
如果您想坚持使用 gcc,只需将“clang”替换为“gcc”。
回答by Nic Fox
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:
可能对此没有任何直接帮助,但是您可以为常用的 xcrun 命令设置别名,以便在其他进程调用 gcc、make、gdb、git 等时使用 Xcode 版本:
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.
如果您愿意,您可以将别名放入 .bashrc 文件中,以便它持续存在,并根据需要从 .bash_profile 中获取。
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.
这一切的好处是你不需要安装 Xcode 命令行工具,还可以避免使用包管理器,节省空间,降低复杂性,并利用 Xcode 自动为你更新这些工具。
回答by josealobato
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.
使用 xcrun --sysroot 对我使用 10.8 不起作用。查看 clang 文档,我发现 -isysroot 是在这种情况下使用的选项。
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'