【问题标题】:fatal error: stdio.h: No such file or directory致命错误:stdio.h:没有这样的文件或目录
【发布时间】:2018-12-29 23:40:59
【问题描述】:

我对 mac Mojave 10.14.1 上的 #include <stdio.h> 有疑问

我有一个默认的 gcc 编译器和其他 gcc 编译器。

DorothyeMacBook:Desktop dorothy$ which gcc
/usr/local/bin/gcc

版本是

DorothyeMacBook:Desktop dorothy$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-apple-darwin15.6.0/7.1.0/lto-wrapper
Target: x86_64-apple-darwin15.6.0
Configured with: ../gcc-7.1.0/configure --enable-languages=c++,fortran
Thread model: posix
gcc version 7.1.0 (GCC) 

现在我在路上: /用户/dorothy/桌面

我在库中有 stdio.h

DorothyeMacBook:Desktop dorothy$ find /usr -name "stdio.h"
find: /usr/sbin/authserver: Permission denied
/usr/local/include/c++/7.1.0/tr1/stdio.h
/usr/local/include/c++/4.9.2/tr1/stdio.h
/usr/local/lib/gcc/x86_64-apple-darwin15.6.0/7.1.0/include/ssp/stdio.h
/usr/local/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2/include/ssp/stdio.h
/usr/local/Cellar/gcc/8.2.0/include/c++/8.2.0/tr1/stdio.h
/usr/local/Cellar/gcc/8.2.0/lib/gcc/8/gcc/x86_64-apple-darwin18.2.0/8.2.0/include/ssp/stdio.h
/usr/local/Cellar/gcc/8.2.0/lib/gcc/8/gcc/x86_64-apple-darwin18.2.0/8.2.0/include-fixed/stdio.h

但是当我在当前路径/Users/dorothy/Desktop Terminal下编译文件时会给我一个错误:

DorothyeMacBook:Desktop dorothy$ gcc  inverse.c -o inv
inverse.c:1:10: fatal error: stdio.h: No such file or directory
 #include <stdio.h>
          ^~~~~~~~~
compilation terminated.

【问题讨论】:

  • gcc 仅针对 c++ 和 fortran 构建,但您正尝试将其用作 c 编译器,这似乎有点奇怪。如果您将inverse.c 重命名为inverse.cpp 是否有效?
  • Configured with: ../gcc-7.1.0/configure --enable-languages=c++,fortran 是的,这看起来很奇怪,我的列表中有c,没有它我无法判断是否支持stdio.h。从错误信息来看,我认为不是。
  • 你安装了xcode吗?
  • @JonathanLeffler:你为什么认为我们是重复的?软件安装有很多方法可能会搞砸。那里的问题似乎是 Xcode/Apple 工具安装损坏。这个问题似乎涉及到一些 GCC 版本,这是不同的(Apple 现在使用 Clang)。
  • 请参阅Can't compile C program on a Mac after upgrade to Mojave 以了解可能的解决方案。并非所有人都同意您的问题将通过那里的步骤解决,尽管我强烈怀疑您所看到的就是我所看到的,以及该问题的 OP 所看到的——系统标头已被 Apple 从 /usr/include 中删除在升级到 Mojave 期间。

标签: c compiler-errors


【解决方案1】:

在我的 Mac 上将 gcc 与 stdio.h 链接仍然存在问题。但是,我找到了另一种使用gcc 编译程序的方法。我在终端输入了brew install gcc,这意味着将安装一个新的gcc8.0.2。(这是我的情况,我不知道你会得到什么gcc ) 然后我使用gcc-8 而不是gcc 来编译我的程序。这样,新的gcc8.0.2就被调用了。

您可以通过转到/usr/local/bin 检查 gcc 变体的命令名称gcc 命令的所有名称都列在那里。至于我,那些是

g++                 
g++-8               
gcc                 
gcc-8 

如果您还有其他问题,可以联系我,我会提供更多详细信息。

【讨论】:

    【解决方案2】:

    @Jonathan Leffler 是对的。苹果又为我们做了一个大蛋糕。 由于 /usr/include 已被移动(现在它在安装命令行工具后位于 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include 中),旧版本的 gcc build 找不到最底层的包含文件。 这是个问题。 (他们脑子里有什么)所以只有你是开发者,你使用/usr/include?

    这不是类 Unix。

    这就是发生的事情。对于gcc-4.9(下面的gcc-4.9和gcc-9都是brew gcc.),include搜索路径是:

    enter ignoring nonexistent directory "/usr/include"
    #include "..." search starts here:
    #include <...> search starts here:
    .
    /usr/local/include
    /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
    /usr/local/Cellar/gcc@4.9/4.9.4_1/lib/gcc/4.9/gcc/x86_64-apple-darwin17.3.0/4.9.4/include
    /usr/local/Cellar/gcc@4.9/4.9.4_1/include
    /usr/local/Cellar/gcc@4.9/4.9.4_1/lib/gcc/4.9/gcc/x86_64-apple-darwin17.3.0/4.9.4/include-fixed
    /System/Library/Frameworks
    /Library/Frameworks
    End of search list. here
    

    有系统级文件stdio.h,所以找不到stdio.h。

    问题在:

    inverse.c:1:10: fatal error: stdio.h: No such file or directory
    #include <stdio.h>
          ^~~~~~~~~
    compilation terminated.
    

    是:

    参考/usr/local/Cellar/gcc@4.9/4.9.4_1/lib/gcc/4.9/gcc/x86_64-apple-darwin17.3.0/4.9.4/include/ssp/stdio.h

    #ifndef _SSP_STDIO_H
    #define _SSP_STDIO_H 1
    
    #include <ssp.h>
    #include_next <stdio.h>
    

    #include_next 表示包含下一个名称stdio.h,这个只是一个向导。

    给出一个已弃用的镜头。 cp /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include 中的所有文件到/usr/local/Cellar/gcc@4.9/4.9.4_1/lib/gcc/4.9/gcc/x86_64-apple-darwin17。 3.0/4.9.4/include-fixed,gcc-4.9 将再次工作。

    但是对于 gcc-9,路径问题很好。见:

    #include "..." search starts here:
    #include <...> search starts here:
    .
    /usr/local/include
    /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include # key path
    /usr/local/Cellar/gcc/9.1.0/lib/gcc/9/gcc/x86_64-apple-darwin18/9.1.0/include
    /usr/local/Cellar/gcc/9.1.0/lib/gcc/9/gcc/x86_64-apple-darwin18/9.1.0/include-fixed
    /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks
    End of search list.
    

    其实这个bug可以通过重新安装xcode命令行工具来修复,之后gcc-4.9就可以再次工作了。

    【讨论】:

      猜你喜欢
      • 2013-11-04
      • 2021-04-29
      • 2014-05-25
      • 2023-03-30
      • 2020-01-25
      • 2017-05-09
      • 2021-05-15
      • 2014-10-31
      • 2022-01-11
      相关资源
      最近更新 更多