升级到Mojave后无法在Mac上编译C程序

Max*_*xxx 145 c macos terminal gcc

我在终端上使用了gcc命令来编译C程序但突然间,在我的Mac操作系统更新后(到macOS 10.14 Mojave和XCode 10.0),我开始收到消息:

test.c:8:10: fatal error: stdio.h: No such file or directory
#include <stdio.h>
         ^~~~~~~~~
compilation terminated.
Run Code Online (Sandbox Code Playgroud)

我已经安装了gcc,因为我可以找到它/usr/local/bin并且那里确实有一个gcc.我尝试在我的其他iMac上运行相同的文件,它没有任何问题.

我试过运行xcode-select --install它已经安装好了,因此它没有解决我现在遇到的问题.我猜测路径搞砸了,因为gcc在我开始复制并粘贴其他资源中的一些命令来解决此问题后,它似乎无法找到.

希望对此有所帮助.

Jon*_*ler 250

从相当广泛的评论中提取半连贯的答案......

前言

通常,xcode-select --install这是一个正确的解决方案,但这次似乎没有帮助.您是否尝试过运行主Xcode GUI界面?它可能会为您安装一些额外的软件并进行清理.我在安装Xcode 10.0后做了这件事,但是在升级到Mojave之前一周或更长时间.

我观察到如果安装了GCC /usr/local/bin,你可能没有使用Xcode的GCC; 通常安装在/usr/bin.

我也更新到macOS 10.14 Mojave和Xcode 10.0.但是,系统/usr/bin/gcc和系统/usr/bin/clang都适用于我(Apple LLVM version 10.0.0 (clang-1000.11.45.2) Target: x86_64-apple-darwin18.0.0对于两者而言).我的家庭内置GCC 8.2.0没有找到标题/usr/include,这与你的问题并行,但/usr/local/bin/gcc没有找到标题.

我做了一些比较,我的Mojave机器根本没有/usr/include,但/usr/bin/clang能够编译好.标题(_stdio.h带有前导下划线)在我的旧书中/usr/include; 它现在缺失了(因此我的GCC 8.2.0问题).我跑了xcode-select --install,它说" xcode-select: note: install requested for command line developer tools"然后运行了一个GUI安装程序,它向我展示了我同意的许可证,并且下载并安装了命令行工具 - 或者它声称.

然后我运行Xcode GUI(命令空间,Xcode,返回),它说它需要安装更多的软件,但仍然没有/usr/include.但我可以编译/usr/bin/clang/usr/bin/gcc- 并且-v选项表明他们正在使用

open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
Run Code Online (Sandbox Code Playgroud)

工作方案

然后Maxxx 指出:

我找到了办法.如果我们使用Xcode 10,您会注意到如果您导航到/usrFinder中,您将不会再看到名为"include"的文件夹,这就是为什么终端会抱怨没有包含在内的头文件'include'文件夹.在Xcode 10.0发行说明中,它说有一个包:

InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Run Code Online (Sandbox Code Playgroud)

并且您应该安装该软件包以安装该/usr/include文件夹.然后你应该好好去.

当所有其他方法都失败时,请阅读手册,或者在本例中阅读发行说明.我发现Apple希望拒绝他们的Unix传统,我并不会感到非常惊讶,但我很失望.如果他们小心,他们可能会把我赶走.感谢您的信息.

在命令行使用以下命令安装了软件包后,我/usr/include又一次,我的GCC 8.2.0再次运行.

/Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg 
Run Code Online (Sandbox Code Playgroud)

下载命令行工具

正如Vesal在一篇有价值的评论中指出的那样,你需要在Mojave 10.14上下载Xcode 10.1的命令行工具包,你可以这样做:

您需要使用Apple ID登录才能获得下载.完成下载后,安装命令行工具包.然后按照"工作解决方案"一节中的说明安装标头.

这在Mojave 10.14.1上对我有用.我之前一定已经下载了这个,但是当我回答这个问题时我已经忘记了.

  • 他妈的苹果.他们不能只留下有用的东西,他们必须移动东西,添加步骤并创造流失. (11认同)
  • ***GRUMP !!!***随着10.14.1更新,情况再次发生了变化 - 或者至少,它们似乎再次发生了变化.对我来说,o/s更新似乎吹走了`/ usr/include`,上面答案中列出的包不在`/ Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg`中. `xcode-select --install`表示命令行工具目前不可用. (8认同)
  • 希望搜索引擎能找到这个答案,上面的指令解决了一个致命的错误:bits/ctype_base.h:没有这样的文件或目录:#include <bits/ctype_base.h>`使用libstdc ++编译GCC 7.4.0时在莫哈韦对我来说 (7认同)
  • 为我解决了!!! 非常感谢您花时间写这篇文章! (5认同)
  • 这行`open / Library / Developer / CommandLineTools / Packages / macOS_SDK_headers_for_macOS_10.14.pkg`救了我。我陷入了老鼠的巢穴,试图用“ llvm”和“ readline”编译“ crystal”,然后使“ ruby​​”环境无法正常工作,这使我从灭绝的边缘退缩了。谢谢。 (4认同)
  • 啊,谢谢你写这个.非常感激.干杯! (3认同)
  • 如果Apple能够采用一种方法来更新头文件,而不是在每次**更新操作系统时提出不同的内容,那就太棒了.理想情况下,标题应该在安装时安装,但这显然太多了......谢谢Jonathan:这解决了我的问题. (3认同)
  • 从这里下载命令行工具后:https://developer.apple.com/download/more/(需要使用苹果ID登录),这最终对我有用. (2认同)
  • 在我的两台机器(工作和家庭)之一上,当我升级到Xcode 10.2.1(在macOS 10.14.4上)时,我不得不重新运行“ open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg”,因为安装后缺少目录“ / usr / include”。IIRC,在那台机器上,当提示更新可用时,我手动启动了更新。在另一台机器上,我不必手动启动更新(在我看的时候它已经完成了),但是我仍然有`/ usr / include`的位置。 (2认同)
  • 从 Xcode 11.1 开始,“/usr/include”目录确实被删除了,并且用于重新生成它的“macOS_SDK_headers_for_macOS_10.14.pkg”文件已被删除(正如 Xcode 10.0 发行说明所警告的那样)。谢谢,苹果!*叹* (2认同)

小智 23

在尝试了我在这里和网上找到的每个答案之后,我仍然会收到一些丢失标题的错误.在尝试编译pyRFR时,我收到了有关stdexcept未找到的错误,显然未/usr/include与其他标头一起安装.但是,我发现它藏在Mojave中的位置并将其添加到我的~/.bash_profile文件末尾:

export CPATH=/Library/Developer/CommandLineTools/usr/include/c++/v1

完成后,我现在可以编译pyRFR和其他C/C++程序.根据echo | gcc -E -Wp,-v -,gcc在旧位置查找这些标题(没有/c++/v1),但不是新位置,所以将其添加到CFLAGS修复它.

  • 邪恶的肉汤 (2认同)

小智 12

问题是Xcode(尤其是Xcode 10.x)尚未安装所有内容,因此请确保已安装命令行工具,并在终端外壳中键入以下内容:

xcode-select-安装

还要启动Xcode并确保已安装所有必需的安装(如果未安装,则将得到提示。)并且由于Xcode 10没有安装完整的Mac OS SDK,请在以下位置运行安装程序

/Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg

因为Xcode 10未安装此软件包。


小智 12

当你

  • 更新为 Mojave 10.14.6
  • /usr/include再次被删除
  • @ Jonathan-lefflers 答案中提到的软件包已不存在, The file /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg does not exist.并且
  • Xcode抱怨命令行工具已经安装 xcode-select --install xcode-select: error: command line tools are already installed, use "Software Update" to install updates

然后,帮助我恢复上述软件包的原因是删除了整个CommandLineTools文件夹 (sudo) rm -rf /Library/Developer/CommandLineTools并重新安装xcode-select --install

  • 对我来说,以下内容有效: `export CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include` (3认同)
  • 每次Apple发布系统更新时,我都必须以不同的方式解决Clion的这个问题。 (2认同)

Rob*_*Rob 8

我在这个GitHub 评论中找到了很好的解决方案和解释。诀窍:

make SDKROOT=`xcrun --show-sdk-path` MACOSX_DEPLOYMENT_TARGET=
Run Code Online (Sandbox Code Playgroud)

做了工作。


Hap*_*ace 7

ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk'可能会帮助你。它解决了我的问题。


JL *_*ret 5

注意:在macOS Catalina 10.15的切换/一般可用性之前,以下内容可能与上下文和时间高度相关。新的笔记本电脑。我正在写2019年10月1日。

我认为,这些特殊情况是造成我制造问题的原因。它们可能不适用于大多数其他情况。

内容:

  • macos 10.14.6 Mojave,Xcode 11.0,就在macos Catalina 10.15发布之前。新购买的Macbook Pro。

  • 失败pip install psycopg2,基本上是从源代码编译的Python包。

  • 我已经在此处给出的答案中进行了一些建议的调整。

我的错误:

pip install psycopg2
Collecting psycopg2
  Using cached https://files.pythonhosted.org/packages/5c/1c/6997288da181277a0c29bc39a5f9143ff20b8c99f2a7d059cfb55163e165/psycopg2-2.8.3.tar.gz
Installing collected packages: psycopg2
  Running setup.py install for psycopg2 ... error
    ERROR: Command errored out with exit status 1:
     command: xxxx/venv/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/bk/_1cwm6dj3h1c0ptrhvr2v7dc0000gs/T/pip-install-z0qca56g/psycopg2/setup.py'"'"'; __file__='"'"'/private/var/folders/bk/_1cwm6dj3h1c0ptrhvr2v7dc0000gs/T/pip-install-z0qca56g/psycopg2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/bk/_1cwm6dj3h1c0ptrhvr2v7dc0000gs/T/pip-record-ef126d8d/install-record.txt --single-version-externally-managed --compile --install-headers xxx/venv/include/site/python3.6/psycopg2


...
/usr/bin/clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -pipe -Os -isysroot/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -DPSYCOPG_VERSION=2.8.3 (dt dec pq3 ext lo64) -DPG_VERSION_NUM=90615 -DHAVE_LO64=1 -I/Users/jluc/kds2/py2/venv/include -I/opt/local/Library/Frameworks/Python.framework/Versions/3.6/include/python3.6m -I. -I/opt/local/include/postgresql96 -I/opt/local/include/postgresql96/server -c psycopg/psycopgmodule.c -o build/temp.macosx-10.14-x86_64-3.6/psycopg/psycopgmodule.o

    clang: warning: no such sysroot directory: 
'/Applications/Xcode.app/Contents/Developer/Platforms
                              ?the real error?
/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk' [-Wmissing-sysroot]
    In file included from psycopg/psycopgmodule.c:27:
    In file included from ./psycopg/psycopg.h:34:
    /opt/local/Library/Frameworks/Python.framework/Versions/3.6/include/python3.6m/Python.h:25:10: fatal error: 'stdio.h' file not found
                             ? what I thought was the error ?
    #include <stdio.h>
             ^~~~~~~~~
    1 error generated.

    It appears you are missing some prerequisite to build the package 


Run Code Online (Sandbox Code Playgroud)

到目前为止,我没有做任何事情:

  • xcode-select --install
  • 安装的xcode
  • open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg

在stdio.h上仍然是相同的错误。

它存在于许多地方:

(venv) jluc@bemyerp$ mdfind -name stdio.h
/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/stdio.h
/usr/include/_stdio.h
/usr/include/secure/_stdio.h
/usr/include/stdio.h  ?  I believe this is the one that's usually missing.
                            but I have it.
/usr/include/sys/stdio.h
/usr/include/xlocale/_stdio.h
Run Code Online (Sandbox Code Playgroud)

因此,我们转到第一个clang抱怨的目录并查看:

(venv) jluc@gotchas$ cd /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs
(venv) jluc@SDKs$ ls -l
total 0
drwxr-xr-x  8 root  wheel  256 Aug 29 23:47 MacOSX.sdk
drwxr-xr-x  4 root  wheel  128 Aug 29 23:47 DriverKit19.0.sdk
drwxr-xr-x  6 root  wheel  192 Sep 11 04:47 ..
lrwxr-xr-x  1 root  wheel   10 Oct  1 13:28 MacOSX10.15.sdk -> MacOSX.sdk  
drwxr-xr-x  5 root  wheel  160 Oct  1 13:34 .
Run Code Online (Sandbox Code Playgroud)

嗯,我们有一个用于MacOSX10.15.sdk的符号链接,但没有一个用于MacOSX10.14.sdk的符号链接。这又是我的第一个clang错误:

clang: warning: no such sysroot directory: '/Applications/Xcode.app/.../Developer/SDKs/MacOSX10.14.sdk' [-Wmissing-sysroot]

我的猜测是,苹果公司在他们的xcode配置上大开眼界,并且已经认为他们在Catalina上。由于它是一台新的Mac,因此没有适用于10.14的旧配置。

修复:

让我们以10.15的相同方式对10.14进行符号链接:

ln -s MacOSX.sdk/ MacOSX10.14.sdk

顺便说一句,如果我转到该sdk目录,则会发现:

...
./usr/include/sys/stdio.h
./usr/include/stdio.h
....
Run Code Online (Sandbox Code Playgroud)

结果:

pip install psycopg2 作品。

注意:实际的pip install命令没有引用MacOSX10.14.sdk,后者稍后出现,可能是通过Python安装机制自省了OS版本。