gcc48(从Homebrew下载)找不到stdio.h。

3

我正在使用OSX Yosemite,我从homebrew安装了gcc-4.8

以下程序在编译时失败(例如:gcc-4.8 main.c):

#include <stdio.h>    

int main(void)
{
    printf("hello world");

    return 0;
}

出现错误:

main.c:8:19: fatal error: stdio.h: No such file or directory
 #include <stdio.h>
                   ^
compilation terminated.

并且。
 $`gcc-4.8 -print-prog-name=cc1` -v

产出
ignoring nonexistent directory "/usr/local/Cellar/gcc48/4.8.4/lib/gcc/4.8/gcc/x86_64-apple-darwin14.3.0/4.8.4/../../../../../../x86_64-apple-darwin14.3.0/include"
ignoring nonexistent directory "/usr/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/Cellar/gcc48/4.8.4/lib/gcc/4.8/gcc/x86_64-apple-darwin14.3.0/4.8.4/include
 /usr/local/include
 /usr/local/Cellar/gcc48/4.8.4/include
 /usr/local/Cellar/gcc48/4.8.4/lib/gcc/4.8/gcc/x86_64-apple-darwin14.3.0/4.8.4/include-fixed
 /System/Library/Frameworks
 /Library/Frameworks
End of search list.

并且

$ find /usr/local/ -name "stdio.h" -print

产量
/usr/local//Cellar/gcc/5.2.0/include/c++/5.2.0/tr1/stdio.h
/usr/local//Cellar/gcc/5.2.0/lib/gcc/5/gcc/x86_64-apple-darwin14.4.0/5.2.0/include/ssp/stdio.h
/usr/local//Cellar/gcc48/4.8.4/include/c++/4.8.4/tr1/stdio.h
/usr/local//Cellar/gcc48/4.8.4/lib/gcc/4.8/gcc/x86_64-apple-darwin14.3.0/4.8.4/include/ssp/stdio.h
/usr/local//include/c++/4.8.4/tr1/stdio.h
/usr/local//lib/gcc/4.8/gcc/x86_64-apple-darwin14.3.0/4.8.4/include/ssp/stdio.h

现在我知道我可以传递-I /path/to/directory让编译器找到正确的头文件,但是这是stdio.h!我不想为了一些小程序而这样做。有什么方法可以解决这个问题吗?


1
你安装了XCode吗?还有XCode的命令行工具吗?命令行工具有可能会将头文件放在原位(也就是/usr/include)。XCode将其头文件隐藏在某个神秘的地方。当我运行clang -v编译一个文件时,它列出了/usr/local/include/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2/include/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/usr/include,但我已经安装了命令行工具。 - Jonathan Leffler
这引出了另一个问题...为什么要使用gcc,而XCode安装了一个完全可用的、事实上更加功能强大的编译器(例如,您将拥有更好的泄漏检测软件)... - autistic
1
Seb - 很好的问题。因为我习惯使用gcc开发代码,我更愿意使用我熟悉且在Linux和OSX上容易获取的编译器(和选项)来开发代码。另一个抱怨是clang默认篡夺了OSX的gcc命令,而事实上它并不是gcc。这从根本上激怒了我。 - irritable_phd_syndrome
正如其他Jonathan所说,您需要“XCode的命令行工具”来提供/usr中的C库头文件(以及提供汇编器和链接器,我想)。 - Jonathan Wakely
Jons- 我最近从Mavericks升级到了Yosemite。我知道之前已经安装了XCode,但今晚我会检查一下。也许升级损坏了这个功能。 - irritable_phd_syndrome
显示剩余2条评论
1个回答

1

我最终删除了brew公式并重新安装了它。现在它可以工作了,但我仍然不清楚它何时/在哪里被破坏了。

编辑 事实证明,这个问题现在已经发生了好几次。每当我升级OSX或Xcode时都会发生。


我在Sierra上遇到了同样的问题,已经重新安装了两次,但没有用。 - Jeff Hammond

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接