美文网首页
xcode升级到10.1后stdio.h找不到的解决方案

xcode升级到10.1后stdio.h找不到的解决方案

作者: typesafe | 来源:发表于2018-11-19 22:30 被阅读0次

    最近把macos升级到mojave,顺便把xcode也升到了10.1。结果编译C代码的时候,发现找不到stdio.h了:

    <stdin>:1:19: 致命错误:stdio.h:No such file or directory
    

    头文件包含路径有问题吗?于是用gcc搜索stdio.h(gcc几个编译选项的解释见最下面的附录):

    $ echo "#include <stdio.h>" | gcc -v -x c -
    使用内建 specs。
    COLLECT_GCC=gcc
    COLLECT_LTO_WRAPPER=/usr/local/gcc-6.2.0/libexec/gcc/x86_64-apple-darwin16.1.0/6.2.0/lto-wrapper
    目标:x86_64-apple-darwin16.1.0
    配置为:../configure --prefix=/usr/local/gcc-6.2.0 --with-gmp=/usr/local/gmp-6.1.0 --with-mpfr=/usr/local/mpfr-3.1.4 --with-mpc=/usr/local/mpc-1.0.3 --disable-multilib --with-sytem-zlib --enable-languages=c,c++
    线程模型:posix
    gcc 版本 6.2.0 (GCC) 
    COLLECT_GCC_OPTIONS='-v' '-mmacosx-version-min=10.14.0' '-mtune=core2'
     /usr/local/gcc-6.2.0/libexec/gcc/x86_64-apple-darwin16.1.0/6.2.0/cc1 -quiet -v -D__DYNAMIC__ - -fPIC -quiet -dumpbase - -mmacosx-version-min=10.14.0 -mtune=core2 -auxbase - -version -o /var/folders/23/cb_mbvwd6hg6sj5543x3dj_h0000gn/T//ccsU3P4Z.s
    GNU C11 (GCC) 版本 6.2.0 (x86_64-apple-darwin16.1.0)
        由 GNU C 版本 6.2.0 编译,GMP 版本 6.1.0,MPFR 版本 3.1.4,MPC 版本 1.0.3,isl 版本 none
    GGC 准则:--param ggc-min-expand=100 --param ggc-min-heapsize=131072
    忽略不存在的目录“/usr/local/gcc-6.2.0/lib/gcc/x86_64-apple-darwin16.1.0/6.2.0/../../../../x86_64-apple-darwin16.1.0/include”
    忽略不存在的目录“/usr/include”
    #include "..." 搜索从这里开始:
    #include <...> 搜索从这里开始:
     /usr/local/gcc-6.2.0/lib/gcc/x86_64-apple-darwin16.1.0/6.2.0/include
     /usr/local/include
     /usr/local/gcc-6.2.0/include
     /usr/local/gcc-6.2.0/lib/gcc/x86_64-apple-darwin16.1.0/6.2.0/include-fixed
     /System/Library/Frameworks
     /Library/Frameworks
    搜索列表结束。
    GNU C11 (GCC) 版本 6.2.0 (x86_64-apple-darwin16.1.0)
        由 GNU C 版本 6.2.0 编译,GMP 版本 6.1.0,MPFR 版本 3.1.4,MPC 版本 1.0.3,isl 版本 none
    GGC 准则:--param ggc-min-expand=100 --param ggc-min-heapsize=131072
    Compiler executable checksum: 012c530f028eade4bd44e1c5c6ad79c2
    <stdin>:1:19: 致命错误:stdio.h:No such file or directory
    编译中断。
    

    结果竟然告诉我,/usr/include目录不存在!
    stackoverflow上查了下,原来是xcode挖的坑:

    The Command Line Tools package installs the macOS system headers inside the macOS SDK. Software that compiles with the installed tools will search for headers within the macOS SDK provided by either Xcode at:
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk
    or the Command Line Tools at:
    /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
    depending on which is selected using xcode-select.
    The command line tools will search the SDK for system headers by default. However, some software may fail to build correctly against the SDK and require macOS headers to be installed in the base system under /usr/include. If you are the maintainer of such software, we encourage you to update your project to work with the SDK or file a bug report for issues that are preventing you from doing so. As a workaround, an extra package is provided which will install the headers to the base system. In a future release, this package will no longer be provided. You can find this package at:
    /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
    To make sure that you're using the intended version of the command line tools, run xcode-select -s <path to Xcode> or xcode select -s /Library/Developer/CommandLineTools after installing.

    解决方案有两个,更简单的是安装macOS_SDK_headers_for_macOS_10.14.pkg,不过xcode若干个版本后就不再支持了。
    安装方法如下

    $ cd /Library/Developer/CommandLineTools/Packages/
    $ open macOS_SDK_headers_for_macOS_10.14.pkg
    

    然后会弹出一个安装界面,一直点继续,最后/usr/include总算恢复正常了。

    附录

    gcc的编译选项

    • '-v': 打印gcc版本信息和编译过程的详细信息
    • '-x c': 指定以C语言来编译
    • '-': 指定以标准输入stdin作为输入,而不是文件

    相关文章

      网友评论

          本文标题:xcode升级到10.1后stdio.h找不到的解决方案

          本文链接:https://www.haomeiwen.com/subject/jukzfqtx.html