升级到 Mojave 后无法在 Mac 上编译 C 程序 [英] Can't compile C program on a Mac after upgrade to Mojave

查看:23
本文介绍了升级到 Mojave 后无法在 Mac 上编译 C 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have used the gcc command on the terminal to compile C programs but all of a sudden, after an update to my Mac's OS (to macOS 10.14 Mojave, and XCode 10.0), I started receiving the message:

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

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

I already have gcc installed as I can find it in /usr/local/bin and there really is a gcc in there. I tried running the same file on my other iMac and it worked without any issue.

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

I tried running xcode-select --install and it already was installed, hence it didn't fix the issue I'm having now. I'm guessing that the path is messed up as it doesn't seem like it can find gcc after I started copying and pasting some commands from other resources to solve this issue.

希望得到一些帮助.

推荐答案

TL;DR

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

TL;DR

Make sure you have downloaded the latest 'Command Line Tools' package and run this from a terminal (command line):

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 之前很久.

Very often, xcode-select --install has been the correct solution, but it does not seem to help this time. Have you tried running the main Xcode GUI interface? It may install some extra software for you and clean up. I did that after installing Xcode 10.0, but a week or more ago, long before upgrading to Mojave.

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

I observe that if your GCC is installed in /usr/local/bin, you probably aren't using the GCC from Xcode; that's normally installed in /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) 目标:x86_64-apple-darwin18.0.0 两者都适用.)我的自制 GCC 8.2.0 有问题,在 /usr/include 中找不到标题,这与 /usr/local/bin/gcc 找不到标题的问题是平行的.

I too have updated to macOS 10.14 Mojave and Xcode 10.0. However, both the system /usr/bin/gcc and system /usr/bin/clang are working for me (Apple LLVM version 10.0.0 (clang-1000.11.45.2) Target: x86_64-apple-darwin18.0.0 for both.) I have a problem with my home-built GCC 8.2.0 not finding headers in /usr/include, which is parallel to your problem with /usr/local/bin/gcc not finding headers either.

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

I've done a bit of comparison, and my Mojave machine has no /usr/include at all, yet /usr/bin/clang is able to compile OK. A header (_stdio.h, with leading underscore) was in my old /usr/include; it is missing now (hence my problem with GCC 8.2.0). I ran xcode-select --install and it said "xcode-select: note: install requested for command line developer tools" and then ran a GUI installer which showed me a licence which I agreed to, and it downloaded and installed the command line tools — or so it claimed.

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

I then ran Xcode GUI (command-space, Xcode, return) and it said it needed to install some more software, but still no /usr/include. But I can compile with /usr/bin/clang and /usr/bin/gcc — and the -v option suggests they're using

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

工作解决方案

然后 Maxxx 注意到:

我找到了方法.如果我们使用 Xcode 10,您会注意到,如果您导航到 Finder 中的 /usr,您将不会再看到名为include"的文件夹,这就是终端抱怨缺少包含在include"文件夹中的头文件.在 Xcode 10.0 Release Notes 中,它说有一个包:

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 

并且您应该安装该软件包以安装 /usr/include 文件夹.那你就可以出发了.

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

当所有其他方法都失败时,请阅读手册,或者在这种情况下阅读发行说明.发现 Apple 想要背弃他们的 Unix 传统,我并不感到非常惊讶,但我很失望.如果他们小心点,他们可以把我赶走.谢谢你的信息.

When all else fails, read the manual or, in this case, the release notes. I'm not dreadfully surprised to find Apple wanting to turn their backs on their Unix heritage, but I am disappointed. If they're careful, they could drive me away. Thank you for the information.

在命令行中使用以下命令安装软件包后,我再次拥有 /usr/include,并且我的 GCC 8.2.0 再次运行.

Having installed the package using the following command at the command line, I have /usr/include again, and my GCC 8.2.0 works once more.

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

下载命令行工具

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

Downloading Command Line Tools

As Vesal points out in a valuable comment, you need to download the Command Line Tools package for Xcode 10.1 on Mojave 10.14, and you can do so from:

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

You need to login with an Apple ID to be able to get the download. When you've done the download, install the Command Line Tools package. Then install the headers as described in the section 'Working Solution'.

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

This worked for me on Mojave 10.14.1. I must have downloaded this before, but I'd forgotten by the time I was answering this question.

在 2019-05-17 左右,我更新到 Mojave 10.14.4,Xcode 10.2 命令行工具也升级了(或者 Xcode 10.1 命令行工具升级到 10.2).上面显示的 open 命令修复了丢失的标题.将主 Xcode 升级到 10.2,然后重新安装命令行工具和头文件包,可能仍然需要冒险.

On or about 2019-05-17, I updated to Mojave 10.14.4, and the Xcode 10.2 command line tools were also upgraded (or Xcode 10.1 command line tools were upgraded to 10.2). The open command shown above fixed the missing headers. There may still be adventures to come with upgrading the main Xcode to 10.2 and then re-reinstalling the command line tools and the headers package.

在 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 完好无损.

On 2019-07-22, I got notice via the App Store that the upgrade to Xcode 10.3 is available and that it includes SDKs for iOS 12.4, tvOS 12.4, watchOS 5.3 and macOS Mojave 10.14.6. I installed it one of my 10.14.5 machines, and ran it, and installed extra components as it suggested, and it seems to have left /usr/include intact.

当天晚些时候,我发现 macOS Mojave 10.14.6 也可用(系统偏好设置 ⟶ 软件更新),以及命令行实用程序包 IIRC(已自动下载并安装).安装 o/s 更新再次清除了 /usr/include,但答案顶部的 open 命令再次恢复了它.我在 open 命令文件上的日期是 2019 年 7 月 15 日.

Later the same day, I discovered that macOS Mojave 10.14.6 was available too (System Preferences ⟶ Software Update), along with a Command Line Utilities package IIRC (it was downloaded and installed automatically). Installing the o/s update did, once more, wipe out /usr/include, but the open command at the top of the answer reinstated it again. The date I had on the file for the open command was 2019-07-15.

XCode 11.0 的升级(包括适用于 iOS 13、tvOS 13、watchOS 6 和 macOS Catalina 10.15 的 Swift 5.1 和 SDK")已于 2019 年 9 月 21 日发布.我收到了可用更新"的通知,并通过 App Store 应用程序(更新选项卡)将其下载并安装到运行 macOS Mojave 10.14.6 的机器上,没有问题,而且无需使用 /usr/include.安装后立即(在运行应用程序之前),我尝试重新编译并被告知:

The upgrade to XCode 11.0 ("includes Swift 5.1 and SDKs for iOS 13, tvOS 13, watchOS 6 and macOS Catalina 10.15") was released 2019-09-21. I was notified of 'updates available', and downloaded and installed it onto machines running macOS Mojave 10.14.6 via the App Store app (updates tab) without problems, and without having to futz with /usr/include. Immediately after installation (before having run the application itself), I tried a recompilation and was told:

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

Agreeing to the Xcode/iOS license requires admin privileges, please run "sudo xcodebuild -license" and then retry this command.

运行 (sudo xcodebuild -license) 允许我运行编译器.从那以后,我运行该应用程序来安装它需要的额外组件;还是没问题.当我升级到 Catalina 本身时会发生什么还有待观察 - 但我的 macOS Mojave 10.14.6 机器目前都可以(2019-09-24).

Running that (sudo xcodebuild -license) allowed me to run the compiler. Since then, I've run the application to install extra components it needs; still no problem. It remains to be seen what happens when I upgrade to Catalina itself — but my macOS Mojave 10.14.6 machines are both OK at the moment (2019-09-24).

这篇关于升级到 Mojave 后无法在 Mac 上编译 C 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆