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

191

我之前一直使用终端上的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.

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

我尝试运行xcode-select --install,但它已经安装了,因此它没有解决我现在遇到的问题。我猜测路径出了问题,因为似乎无法找到gcc,这是在我从其他资源中复制和粘贴一些命令来解决此问题后出现的。

希望能得到帮助。


7
您可以使用命令 echo "#include <a.h>" | gcc -v -x c - 来查看gcc的搜索路径。 - Matt
4
很多时候,“xcode-select --install”是正确的解决方案。您升级到了哪个操作系统? Mojave 10.14吗? 您安装了哪个版本的XCode? 10.0还是其他版本? - Jonathan Leffler
40
@JonathanLeffler,我找到了一种方法。如果我们使用的是XCode 10,您会发现在Finder中导航到/usr时,您将不再看到一个名为“include”的文件夹,这就是终端抱怨缺少包含在“include”文件夹中的头文件的原因。 在此版本说明中(https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes),您可以导航到/Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg并运行该软件包以安装“include”文件夹。然后您就可以开始使用了。 - Maxxx
2
当一切都失败了,阅读手册!或者阅读发布说明。我并不惊讶于发现苹果想要抛弃Unix的遗产,但我感到失望。如果他们小心一点,他们可能会让我离开。谢谢你提供的信息;我稍后会试验一下(在睡几个小时之后)。 - Jonathan Leffler
17
安装了该软件包(在命令行中执行open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg),我再次拥有了 /usr/include,我的 GCC 8.2.0 也可以正常工作。感谢你的指引,找到它很不错。我建议你将答案写出来,因为可能会有其他人遇到相同的问题。 - Jonathan Leffler
显示剩余8条评论
16个回答

282

简而言之

确保您已下载最新的“命令行工具”包,并从终端(命令行)运行以下命令:

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

有关Catalina的一些信息,请参见在升级到Catalina 10.15后无法在Mac上编译C程序


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

前言

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

我观察到,如果你的 GCC 安装在 /usr/local/bin 中,那么你可能没有使用 Xcode 中的 GCC;它通常安装在 /usr/bin 中。

I too已经升级到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。
我进行了一些比较,我的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 (command-space, Xcode, return),它说需要安装更多的软件,但仍然没有/usr/include。但是我可以用/usr/bin/clang/usr/bin/gcc编译 - -v选项表明他们正在使用

InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

可行解决方案

然后Maxxx noted

I've found a way. If we are using Xcode 10, you will notice that if you navigate to the /usr in the Finder, you will not see a folder called 'include' any more, which is why the terminal complains of the absence of the header files which is contained inside the 'include' folder. In the Xcode 10.0 Release Notes, it says there is a package:

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

and you should install that package to have the /usr/include folder installed. Then you should be good to go.

当一切都失败了,阅读手册或者在这种情况下,阅读发布说明。我并不惊讶于苹果想要背弃他们的Unix传统,但我感到失望。如果他们小心谨慎,他们可能会把我赶走。谢谢您提供的信息。
使用以下命令在命令行中安装软件包后,我再次拥有了/usr/include,并且我的GCC 8.2.0再次工作。
open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg

下载命令行工具

正如Vesal在一条有价值的评论中指出的那样,如果你使用Mojave 10.14操作系统且需要Xcode 10.1版本的命令行工具包,你可以从以下链接进行下载:

你需要使用Apple ID登录才能下载。完成下载后,安装命令行工具包。然后按照“Working Solution”部分的说明安装头文件。
这在Mojave 10.14.1上对我有效。我可能之前已经下载过,但当我回答这个问题时,我忘记了。
升级到Mojave 10.14.4和Xcode 10.2
在2019年5月17日左右,我升级到了Mojave 10.14.4,并且Xcode 10.2命令行工具也进行了升级(或者将Xcode 10.1命令行工具升级到了10.2)。上面显示的“open”命令修复了缺失的头文件。升级主Xcode到10.2然后重新安装命令行工具和头文件包可能还有待冒险。
升级到Xcode 10.3(适用于Mojave 10.14.6)
在2019年7月22日,我通过App Store得到通知,可以升级到Xcode 10.3版本,并且该版本包括iOS 12.4、tvOS 12.4、watchOS 5.3和macOS Mojave 10.14.6的SDK。我在我的10.14.5机器上安装了它,并运行它,按照建议安装了额外的组件,看起来它似乎保留了/usr/include文件夹。
同一天晚些时候,我发现macOS Mojave 10.14.6也可用了(系统偏好设置 ⟶ 软件更新),还有一个命令行工具包(如果我没记错的话,它是自动下载和安装的)。再次安装操作系统更新会清空/usr/include文件夹,但是答案顶部的open命令会重新安装它。我在open命令文件上的日期是2019年7月15日。
升级到XCode 11.0(适用于Catalina 10.15)
XCode 11.0 升级版于 2019-09-21 发布(包含 Swift 5.1 和 iOS 13、tvOS 13、watchOS 6 和 macOS Catalina 10.15 的 SDK)。我在 App Store 应用程序的“更新”选项卡中收到了“可用更新”的通知,并在运行 macOS Mojave 10.14.6 的机器上无需修改 /usr/include,顺利下载并安装。在安装后立即尝试重新编译代码,但出现以下提示:

同意 Xcode/iOS 许可证需要管理员权限,请运行 “sudo xcodebuild -license” 然后重试此命令。

运行 sudo xcodebuild -license 后,编译器可以正常运行。此后,我运行应用程序以安装其所需的额外组件,仍然没有问题。目前我的 macOS Mojave 10.14.6 机器都正常运行(截至 2019-09-24),但升级到 Catalina 后情况尚未确定。


6
解决了!非常感谢您抽出时间来撰写这篇文章! - budekatude
9
Things have changed again with the 10.14.1 update — or, at least, they seem to have changed again. For me, it seems that the o/s update blew away /usr/include, and the package listed in the answer above isn't present in /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg and xcode-select --install says that the command line tools currently aren't available.哎呀,10.14.1更新似乎又出了问题。对我来说,操作系统的更新好像将 /usr/include 文件夹删掉了,而且上面的回答中提到的包在 /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg 中也找不到,运行 xcode-select --install 后提示当前无法使用命令行工具。 - Jonathan Leffler
27
该死的苹果,他们不能只是保持事情正常运转,总要去改变、增加步骤并制造混乱。 - user246672
7
希望搜索引擎能够找到这个答案,上述指令解决了我在Mojave上使用libstdc++编译GCC 7.4.0时遇到的“致命错误:bits/ctype_base.h: 没有那个文件或目录:#include <bits/ctype_base.h>”问题。 - Adam Lindberg
5
这句话“open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg”帮了我很多。当我试图编译“crystal”,却遇到了与“llvm”和“readline”有关的问题,而且我的“ruby”环境也无法正常工作时,这个命令让我重新回到了正常状态。非常感谢。 - Joshua Pinter
显示剩余21条评论

39

尝试了此处和网络上找到的所有解决方案后,我仍然遇到了一些缺失头文件的错误。在尝试编译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中就可以解决问题。


1
什么都不行,但这个可以。非常感谢! - Felix
2
wickedsickbrothnx - Davis Dulin
这对我在macOS Big Sur上也起作用了。 - Pablo Reyes
在我的 MacOS 12.5 上运行良好。 - uPhone

29

当你

  • 升级到 Mojave 10.14.6
  • 你的/usr/include再次被删除
  • @Jonathan-lefflers 答案中提到的软件包不再存在 文件/Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg不存在。并且
  • Xcode提示命令行工具已经安装 xcode-select --install xcode-select: error: 命令行工具已经安装,使用“软件更新”来安装更新

那么,帮助我恢复上述软件包的方法是删除整个CommandLineTools文件夹 (sudo) rm -rf /Library/Developer/CommandLineTools并重新安装它xcode-select --install.


太棒了!按照您的步骤,macOS_SDK_headers_for_macOS_10.14.pkg 可用了,并且命令 open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg 执行了预期的操作。非常感谢!! - Bruno Ambrozio
3
对我来说,以下方法有效:export CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include - rbieber
谢谢!我卡住了,但这对我来说真的很有效,使用的是Mojave 10.4.6和XCode 11.3.1。 - Simeon G
2
每次苹果发布系统更新时,我都必须用不同的方式在Clion中修复这个问题。 - igonejack

12
问题在于Xcode,特别是Xcode 10.x版本,并没有安装所有必要的组件。请确保命令行工具已经安装并输入以下命令在终端中运行:
 
   

xcode-select --install

 
同时,请打开Xcode并确保全部所需的安装已完成(如果未完成,您应该会收到提示)。由于Xcode 10未安装完整的Mac OS SDK,请运行以下安装程序:
 
   

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

 
因为Xcode 10未包含此安装包。

1
这个拯救了我。非常感谢! - Demven Weir

10
我在这个GitHub评论中找到了很好的解决方案和说明。 技巧:
make SDKROOT=`xcrun --show-sdk-path` MACOSX_DEPLOYMENT_TARGET=

工作完成了。

这解决了我的macOS Ventura 13.3.1的问题,谢谢。 - George John

8
"

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

"

你真是个救命恩人。在我升级到XCode 12.2后,这对我很有帮助。 - inthy
就是,同样的问题!在升级到 XCode 12.2 后帮了我大忙! - Aleksey
@BilalAhmedYaseen 你需要自己创建目录结构(我的 ln 命令会自动完成此操作)。 - HappyFace
在 BigSur 11.2.3 上工作过。 - Arijit

7

注意:以下内容可能高度依赖于具体背景和时间,仅适用于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 


到目前为止我所做的事情,仍未解决问题:

  • 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

那么,让我们进入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 .

哈哈,我们有一个针对MacOSX10.15.sdk的符号链接,但没有针对MacOSX10.14.sdk。以下是我的第一个clang错误:

clang: 警告:不存在sysroot目录:'/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
....

成果:

pip install psycopg2 成功了。

注意:实际的pip安装命令没有提到MacOSX10.14.sdk,这可能是由Python安装机制对操作系统版本进行检查后得出的结论。


你使用的是哪个 pip?我认为这可能是关键问题。 - Franklin Yu

4
请确保检查Xcode的偏好设置 -> 位置。
我之前选择的命令行工具适用于旧版本的Xcode(8.2.1而不是10.1)。

3

我遇到了跟OP一样的问题。

问题

cat hello.c

#include <stdlib.h>
int main() { exit(0); }

clang hello.c

/usr/local/include/stdint.h:2:10: error: #include nested too deeply
etc...

尝试的修复方法

我安装了最新版本的XCode,然而发行说明表明之前由Jonathan提供的文件已不再可用。

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

详细信息请参见 https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes ,位于新功能部分。


适用于我的解决方案...

根据此评论中的详细信息

我发现brew doctor 报告我在/usr/local/文件夹中有未使用的包含文件。

因此,我使用用户HowCrazy提供的命令,找到未使用的包含文件并将它们移动到一个临时文件夹中以修复问题。

此处重复一遍...

mkdir /tmp/includes
brew doctor 2>&1 | grep "/usr/local/include" | awk '{$1=$1;print}' | xargs -I _ mv _ /tmp/includes

运行脚本后,包含文件问题已经解决。 nb:我也在这里评论了这个问题。


2

在Big Sur 11.5.2和XCode 12.5.1上,以下步骤可能会有所帮助:

sudo ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk

CMake告诉我,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk不存在,所以我决定创建一个符号链接。

P.S. 在此之前完全删除CommandLineTools目录、运行安装程序、执行brew upgrade等操作都没有帮助。


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