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

查看:73
本文介绍了升级到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的某些信息,请参见


For some information on Catalina, see Can't compile a C program on a Mac after upgrading to Catalina 10.15.

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

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) Target: 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可以编译.我以前的/usr/include中有一个标头(_stdio.h,带下划线).现在丢失了(因此我的GCC 8.2.0问题).我运行了xcode-select --install并说"xcode-select: note: install requested 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发行说明中,它说有一个包装:

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.

如果所有其他方法都失败,请阅读手册或本发行说明.我对发现苹果公司愿意放弃他们的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 指出了一个有价值的

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-07-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的Swift 5.1和SDK,tvOS 13,watchOS 6和macOS Catalina 10.15")于2019-09-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天全站免登陆