Can't compile C program on a Mac after upgrade to Mojave

124,601

Solution 1

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

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


Extracting a semi-coherent answer from rather extensive comments…

Preamble

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.

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.

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.

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.

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

Working solution

Then 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.

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.

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

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:

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'.

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.

Upgrade to Mojave 10.14.4 and 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.

Upgrade to Xcode 10.3 (for Mojave 10.14.6)

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.

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.

Upgrade to XCode 11.0 (for Catalina 10.15)

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:

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

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).

Solution 2

After trying every answer I could find here and online, I was still getting errors for some missing headers. When trying to compile pyRFR, I was getting errors about stdexcept not being found, which apparently was not installed in /usr/include with the other headers. However, I found where it was hiding in Mojave and added this to the end of my ~/.bash_profile file:

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

Having done that, I can now compile pyRFR and other C/C++ programs. According to echo | gcc -E -Wp,-v -, gcc was looking in the old location for these headers (without the /c++/v1), but not the new location, so adding that to CFLAGS fixed it.

Solution 3

When you

  • updated to Mojave 10.14.6
  • your /usr/include was deleted again
  • the package mentioned in @Jonathan-lefflers answer doesn't exist anymore The file /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg does not exist. and
  • Xcode complains that command line tools are already installed xcode-select --install xcode-select: error: command line tools are already installed, use "Software Update" to install updates

Then, what helped me recover the mentioned package, was deleting the whole CommandLineTools folder (sudo) rm -rf /Library/Developer/CommandLineTools and reinstall it xcode-select --install.

Solution 4

The problem is that Xcode, especially Xcode 10.x, has not installed everything, so ensure the command line tools are installed, type this in a terminal shell:

xcode-select --install

also start Xcode and ensure all the required installation is installed ( you should get prompted if it is not.) and since Xcode 10 does not install the full Mac OS SDK, run the installer at

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

as this package is not installed by Xcode 10.

Solution 5

I've found great solution and explanation at this GitHub comment. The trick:

make SDKROOT=`xcrun --show-sdk-path` MACOSX_DEPLOYMENT_TARGET=

Did the job.

Share:
124,601

Related videos on Youtube

Maxxx
Author by

Maxxx

Updated on May 09, 2022

Comments

  • Maxxx
    Maxxx about 2 years

    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.
    

    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.

    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.

    Would like some help on this.

    • Matt
      Matt over 5 years
      You can check the search paths of gcc using echo "#include <a.h>" | gcc -v -x c -
    • Jonathan Leffler
      Jonathan Leffler over 5 years
      Very often, xocde-select --install is the correct solution. Which o/s did you upgrade to? Mojave 10.14? Which XCode have you got installed? 10.0 or another version?
    • Maxxx
      Maxxx over 5 years
      @JonathanLeffler Mojave 10.14, I've installed the latest xcode from the app store version 10.0
    • Jonathan Leffler
      Jonathan Leffler over 5 years
      OK; me too. However, both the system /usr/bin/gcc and system /usr/bin/clang are working for me. (I seem to have a problem with my home-built GCC 8.2.0, but that's tangential to your problem.) (Version: Apple LLVM version 10.0.0 (clang-1000.11.45.2) Target: x86_64-apple-darwin18.0.0 for both.) But the previous installation of the command line tools and headers seems to be OK. Hmmm…I'm going to have to think. I just finished activating Mojave (it wasn't ready when I first commented; it is now; two different Macs, one upgraded, one not).
    • Jonathan Leffler
      Jonathan Leffler over 5 years
      Have you tried running the main XCode GUI interface? It may install some stuff for you and clean up? I did that after installing XCode 10.0, but a week or more ago, long before upgrading to Mojave.
    • Jonathan Leffler
      Jonathan Leffler over 5 years
      OK; 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 GUI stuff to get licence agreement and it downloaded and installed them — or so it claimed. I then ran XCode GUI (again — command-space, xcode, return) and it said it needed to install some more stuff…
    • Jonathan Leffler
      Jonathan Leffler over 5 years
      …and still no /usr/include. Grrrr! 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/XcodeD‌​efault.xctoolchain/u‌​sr/bin. I don't know what's going on here — sorry for the torrent of "as it happens" commentary which isn't getting to an answer.
    • Jonathan Leffler
      Jonathan Leffler over 5 years
      Time passes — I've just had the automatic updater inform me that there's a new version of XCode available and should it install it. I said "Yes". Maybe it fixes this problem.
    • Maxxx
      Maxxx over 5 years
      @JonathanLeffler I've found a way. If we are using XCode 10, you will notice that if you navigate to /usr in the Finder, you will not see a folder called 'include' anymore which is why the terminal complains of the absence of the header files which is contained inside the 'include' folder. In this release statement, developer.apple.com/documentation/xcode_release_notes/… (you navigate to /Library/Developer/CommandLineTools/Packages/macOS_SDK_heade‌​rs_for_macOS_10.14.p‌​kg and run that package to have the 'include' folder installed). Then you should be good to go.
    • Jonathan Leffler
      Jonathan Leffler over 5 years
      When all else fails, read the manual! Or the release notes. I'm not dreadfully surprised to find Apple wanting to turn their backs on the Unix heritage. I am disappointed. If they're careful, they could drive me away. Thank you for the information; I will experiment with it later (after catching a few hours shut-eye).
    • Jonathan Leffler
      Jonathan Leffler over 5 years
      Having installed the package (open /Library/Developer/CommandLineTools/Packages/macOS_SDK_heade‌​rs_for_macOS_10.14.p‌​kg at the command line), I have /usr/include again, and my GCC 8.2.0 works once more. Thanks for the pointer; well done on finding it. I suggest you write up the answer as there'll probably be other people running into the problem.
    • Oleksii Kyslytsyn
      Oleksii Kyslytsyn over 4 years
      Installation CommandLineTools "macOS_SDK_headers_for_macOS_10.14" helps to solve the case for c99 compiler unsupportability during compilations.
    • Robert Metzger
      Robert Metzger over 2 years
      What solved the problem for me was reinstalling the command line tools: sudo rm -rf /Library/Developer/CommandLineTools and sudo xcode-select --install .
  • Maxxx
    Maxxx over 5 years
    ah thanks for writing this. Much appreciated. Cheers!
  • budekatude
    budekatude over 5 years
    Solved it for me!!! Thanks so much for taking the time to write this up!
  • Dietmar Kühl
    Dietmar Kühl over 5 years
    It would just be great if Apple could settle with one approach to update the header files instead of coming up with something different every time the OS gets updated. Ideally, the headers should just be installed when they were installed but that is clearly too much to ask... Thank you Jonathan: that solved the problem for me.
  • keithpjolley
    keithpjolley over 5 years
    I can't figure out if Apple is trying to be more like Sun or like Microsoft when they do stuff like this.
  • Jonathan Leffler
    Jonathan Leffler over 5 years
    GRUMP!!! 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_heade‌​rs_for_macOS_10.14.p‌​kg and xcode-select --install says that the command line tools currently aren't available.
  • Vesal
    Vesal over 5 years
    After downloading the command line tools from here: developer.apple.com/download/more (requires login with apple ID), this finally worked for me.
  • Jonathan Leffler
    Jonathan Leffler over 5 years
    UnGrump!!! — See the new section in the answer about downloading CLT; thanks, @Vesal, for providing the missing link. Now my Mojave 10.14.1 machine is back in business again — and I'm willing to upgrade my other machine to 10.14.1 too.
  • Admin
    Admin over 5 years
    Fcking Apple. They can't just leave things that work, they have to move things, add steps and create churn.
  • Adam Lindberg
    Adam Lindberg over 5 years
    In the hope that search engines will find this answer, the above instructions solved a fatal error: bits/ctype_base.h: No such file or directory: #include <bits/ctype_base.h> when compiling GCC 7.4.0 using libstdc++ on Mojave for me
  • aravind_reddy
    aravind_reddy about 5 years
    after installing the command line tools and installing gcc from conda worked for me thanks a ton
  • Joshua Pinter
    Joshua Pinter about 5 years
    This line open /Library/Developer/CommandLineTools/Packages/macOS_SDK_heade‌​rs_for_macOS_10.14.p‌​kg saved me. I was deep in a rats nest with llvm and readline trying to compile crystal and then having my ruby environment not work, and this brought me back from the edge of extinction. Thank you.
  • Gourav Mahapatra
    Gourav Mahapatra about 5 years
    This worked for me!!! I have been scouring my computer and the internet to find these missing headers!!! Phew. Thanks a ton.
  • Nick Gallimore
    Nick Gallimore about 5 years
    Awesome. This should be in more than just this place on the internet. @GouravMahapatra
  • Nick Gallimore
    Nick Gallimore about 5 years
    @JoshuaPinter where did you find this command? I spent a week searching for this.
  • Felix
    Felix about 5 years
    Nothing worked, but this one did. Thank you very much!!
  • Davis Dulin
    Davis Dulin about 5 years
    wickedsickbrothnx
  • Harsh Patel
    Harsh Patel about 5 years
    Hi @JonathanLeffler I just tried your approach to solve the problem, but even with MacOS 10.14.4 and Xcode 10.2.1, it did not worked.! So I took first approach of downloading the CLT package from Apple developer site and installed according to your guidance. Now it says ` /usr/include/Availability.h:497:18: error: missing binary operator before token "(" ` which is I guess issue because of c++17. Can you please help. You can reproduce results by installing cchardet package.
  • Jonathan Leffler
    Jonathan Leffler about 5 years
    On one of my two machines (work and home), when I upgraded to Xcode 10.2.1 (on macOS 10.14.4), I had to rerun open /Library/Developer/CommandLineTools/Packages/macOS_SDK_heade‌​rs_for_macOS_10.14.p‌​kg because the directory /usr/include was missing after the install. IIRC, on that machine, I kicked off the update manually when something suggested the update was available. On the other machine, I didn't have to start the update manually (it was complete by the time I looked), but I also still had /usr/include left in place.
  • Jonathan Leffler
    Jonathan Leffler about 5 years
    I'm not sure what those observations tell us except that the behaviour is not simple or 100% consistent (which is frustrating for everyone). I was very glad I had my answer to copy the open command from — I'd've upvoted myself if it was allowed (it isn't, so I didn't). I've downloaded the cChardet 2.0.0 source. It doesn't give installation instructions AFAICS, so I've no idea how I'm supposed to compile it. It isn't autoconfigurable — and I don't run setup.py or similar names since I don't know what they do and I therefore don't trust them (and I don't want to read those scripts).
  • Jonathan Leffler
    Jonathan Leffler about 5 years
    I've done some poking around; I see other people have had some problems on macOS 10.14.4. One of the closed issues says "it works" and gives a whole bunch of steps. It looks like you need uchardet as well as cChardet and you may need pyenv or some other Python software around (there was some brew involved). At this stage, I have no clue what's up. You should either add an issue in the cChardet or uchardet projects, or consider asking a question here on SO with appropriate tags. I don't have the context to help, and it isn't clear that this question deals with the problem you face.
  • Jonathan Leffler
    Jonathan Leffler about 5 years
    @HarshPatel: the previous three comments are aimed at you — I forgot to add the @ designation. AFAICS, you're saying that you can run the compiler, but maybe there's a problem with a header file? Have you looked at the Availability.h header around line 497 to see what's up? I see: 497 #if __has_include(<AvailabilityProhibitedInternal.h>). It hasn't yet caused me grief. I note that cChardet 2.1.4 doesn't include build instructions either. I'm sorely tempted to create an issue about "Please add an INSTALL file (or equivalent) that outlines the install process and prerequisites".
  • Demven Weir
    Demven Weir over 4 years
    This saved me. Thanks a lot!
  • Oleksii Kyslytsyn
    Oleksii Kyslytsyn over 4 years
    Installation CommandLineTools "macOS_SDK_headers_for_macOS_10.14" helps to solve the case for c99 compiler unsupportability during compilations
  • vsoftco
    vsoftco over 4 years
    Apple is absolutely ridiculous with those issues; almost every time I upgrade XCode I have to browse the internet to find how to get my good ol' gcc working again!
  • Andrew
    Andrew over 4 years
    As of Xcode 11.1, it appears the /usr/include directory is indeed wiped out, and the macOS_SDK_headers_for_macOS_10.14.pkg file used to regenerate it has been removed (as the Xcode 10.0 release notes warned). Thanks, Apple! sigh
  • Jonathan Leffler
    Jonathan Leffler over 4 years
    @Andrew: yes — see the "Catalina" version of this question which I created (and provisionally answered) yesterday. I am downloading XCode 11.1 to my Catalina machine now, but the 11.0 version is similar to what you say for 11.1.
  • Bruno Ambrozio
    Bruno Ambrozio over 4 years
    Excellent! After your steps, the macOS_SDK_headers_for_macOS_10.14.pkg get available, and the command open /Library/Developer/CommandLineTools/Packages/macOS_SDK_heade‌​rs_for_macOS_10.14.p‌​kg did what was supposed to do. Thanks a million!!
  • rbieber
    rbieber over 4 years
    For me, the following worked: export CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/M‌​acOSX.platform/Devel‌​oper/SDKs/MacOSX10.1‌​5.sdk/usr/include
  • Peter Edwards
    Peter Edwards over 4 years
    On Catalina, I managed to get perl module Text::Levenshtein::XS to compile by setting C_INCLUDE_PATH environment variable to point at XCode 11 header directory $ C_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Pl‌​atforms/MacOSX.platf‌​orm/Developer/SDKs/M‌​acOSX.sdk/System/Lib‌​rary/Perl/5.18/darwi‌​n-thread-multi-2leve‌​l/CORE make
  • Simeon G
    Simeon G over 4 years
    Thank you! I was stuck but this really worked for me with Mojave 10.4.6 and XCode 11.3.1
  • igonejack
    igonejack about 4 years
    Everytime Apple released a system update I have to fix this problem with Clion in different way.
  • Franklin Yu
    Franklin Yu almost 4 years
    Shouldn’t this be a comment?
  • Franklin Yu
    Franklin Yu almost 4 years
    Which pip are you using? I think that could be the key problem.
  • f0nzie
    f0nzie over 3 years
    I was having problems installing git2r in a brand new macOS Mojave 10.14.6 installation. Following the steps in @JonathanLeffler post helped me fix the error: configure: error: cannot run C compiled programs.. I used clang7 and gfortran-6.1
  • inthy
    inthy over 3 years
    You're a life saver. Worked for me after updating to XCode 12.2
  • Aleksey
    Aleksey over 3 years
    Yeah, same here! Helped me after upgrading to XCode 12.2!
  • HappyFace
    HappyFace over 3 years
    @BilalAhmedYaseen You need to create the directory structure yourself (my own ln does this automatically).
  • kevlarr
    kevlarr about 3 years
    @JoshuaPinter "this brought me back from the edge of extinction" was the best and perhaps most personally relevant comment I've seen on SO
  • Pablo Reyes
    Pablo Reyes almost 3 years
    This worked for me too for macOS Big Sur.
  • Arijit
    Arijit almost 2 years
    Worked in BigSur 11.2.3