121

I'm trying to compile a program in C on OS X 10.9 with GCC 4.9 (experimental). For some reason, I'm getting the following error at compile time:

gcc: fatal error: stdio.h: No such file or directory

I then tried a simple Hello World program:

#include <stdio.h>

int main(int argc, const char *argv[])
{
    printf("Hello, world!");
    return 0;
}

Again, upon running gcc -o ~/hello ~/hello.c, I got the same error. I'm using an experimental version of gcc, but it seems implausible that there would be a release which generated errors upon importing stdio. What could be causing this issue, and how can it be fixed?

Jules
  • 14,200
  • 13
  • 56
  • 101
  • 13
    You can see where gcc is looking for header files by doing `echo "#include " | gcc -v -x c -` and examining the search paths. – Christian Ternus Oct 25 '13 at 03:53
  • 1
    Very useful command! It doesn't look like `stdio.h` is in any of GCC's search paths. – Jules Oct 25 '13 at 03:56
  • 2
    Mavericks no longer has a base /usr/include. You need to link it into place from the XCode OS 10.9 SDK. – Yann Ramin Oct 25 '13 at 03:57
  • 4
    BTW, `int main`'s first parameter is an int and not int *argc. – p0lAris Oct 25 '13 at 03:57
  • @YannRamin Thanks, going to try that. – Jules Oct 25 '13 at 04:00
  • @flippex17 Really? I thought it was *arg*ument *c*ount, and then the *arg*ument *v*alues. – Jules Oct 25 '13 at 04:01
  • Yes, it's an `integer` type. – p0lAris Oct 25 '13 at 04:02
  • 1
    @user2615799: It is argument count. It's not pointer to argument count. – rici Oct 25 '13 at 04:02
  • 1
    Alright, thanks for the clarification. @YannRamin I'm not sure how to do that, actually. I'm currently at Xcode > Preferences > Locations, but I can't find any options for library locations. Couldn't this be done by setting the `$LIBRARY_PATH` environment variable? – Jules Oct 25 '13 at 04:08
  • No, since headers are not libraries. `C_INCLUDE_PATH` or the `-isystem` flag are what you want. – Crowman Oct 25 '13 at 04:09
  • I tried setting `C_INCLUDE_PATH` (previously an empty variable) to the location of `stdio.h`, but the issue persists. On a possibly related note, I get no issues when I try importing `stdio.h` in `gdb`. – Jules Oct 25 '13 at 04:17
  • 3
    @user2615799 Its at `sudo ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include /usr/include` – Yann Ramin Oct 25 '13 at 15:11
  • @YannRamin Location changed, but when I fix that I get `ln: /usr/include: Operation not permitted`. Running as *sudo*! – Michael Aug 05 '17 at 06:59

5 Answers5

195

macOS

I had this problem too (encountered through Macports compilers). Previous versions of Xcode would let you install command line tools through xcode/Preferences, but xcode5 doesn't give a command line tools option in the GUI, that so I assumed it was automatically included now. Try running this command:

xcode-select --install

If you see an error message that developer tools are already installed (and still header files can't be found), wipe out any existing one to do a fresh installation:

sudo rm -rf /Library/Developer/CommandLineTools

Ubuntu

(as per this answer)

sudo apt-get install libc6-dev

Alpine Linux

(as per this comment)

apk add libc-dev
Jongwook Choi
  • 8,171
  • 3
  • 25
  • 22
amos
  • 5,092
  • 4
  • 34
  • 43
  • If you're using Xcode 6 beta 5, this command might fail. In that case you need to do `sudo xcode-select -s /Applications/Xcode.app`. When you're done, switch back using `sudo xcode-select -s /Applications/Xcode6-Beta5.app`. – Sjors Provoost Aug 07 '14 at 16:54
  • 1
    Wow. Still working with Xcode 8.0 and Sierra in 2017. – user124384 Apr 22 '17 at 15:45
  • 1
    Alpine guide is extremely usefull for Docker images based on alpine distro, works as wanted – VanDavv Aug 21 '18 at 15:31
  • any suggestion for windows users? I am having this problem on windows – Ratul Hasan Jul 24 '20 at 05:32
  • 1
    For macOS users who're still having problems after installing CLTs https://stackoverflow.com/questions/63342521/clang-on-macos-having-problems-with-its-includes/ –  Oct 16 '20 at 12:48
  • In some situations, you may need to run `sudo rm -rf /Library/Developer/CommandLineTools` to wipe out any existing installations before running `xcode-select --install`. – Jongwook Choi Jan 10 '22 at 16:48
47

Mac OS Mojave

The accepted answer no longer works. When running the command xcode-select --install it tells you to use "Software Update" to install updates.

In this link is the updated method:

Open a Terminal and then:

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

This will open an installation Wizard.

Update 12/2019

After updating to Mojave 10.15.1 it seems that using xcode-select --install works as intended.

Samshel
  • 928
  • 8
  • 12
  • Reinstalling the headers works also when upgrading to Xcode 10.2. – Bojan Dimovski Mar 27 '19 at 12:58
  • When I open macOS_SDK_headers_for_macOS_10.14.pkg, I get a scary Warning from the Installer: "This package is incompatible with this version of macOS and may fail to install." I have Mojave. – Steve Siegel Dec 06 '19 at 17:34
  • @SteveSiegel what Mac OS version are you running? I upgraded to 10.15.1 and I no longer had to do this workaround. Updated my answer to reflect that. – Samshel Dec 07 '19 at 19:57
  • 1
    "updating to Mojave 10.15.1" that's incorrect. @SteveSiegel The pkg is deprecated. SDKROOT should be used. https://stackoverflow.com/a/63343829/10063119 –  Aug 10 '20 at 16:37
32

On Ubuntu:

sudo apt-get install libc6-dev

specially ruby developers that have problem installing gem install json -v '1.8.2' on their VMs

vvvvv
  • 25,404
  • 19
  • 49
  • 81
equivalent8
  • 13,754
  • 8
  • 81
  • 109
  • 1
    Interesting. Thanks. This lib also solves some other issues. Recently, I encountered it trying to run a benchmark for petabyet.com (which uses gcc). – dhaupin Aug 23 '16 at 18:24
  • 17
    On alpine linux I needed `apk add libc-dev` – Shadi Jan 28 '17 at 09:29
  • This is not working for me on ubuntu. I already have libc6-dev installed and it is still looking in the wrong location for the file. – Alexa Kirk Dec 02 '22 at 20:14
4

I know my case is rare, but I'll still add it here for someone who troubleshoots it later. I had a Linux Kernel module target in my Makefile and I tried to compile my user space program together with the kernel module that doesn't have stdio. Making it a separate target solved the problem.

Soid
  • 2,585
  • 1
  • 30
  • 42
2

I had the same problem. I installed "XCode: development tools" from the app store and it fixed the problem for me.

I think this link will help: https://itunes.apple.com/us/app/xcode/id497799835?mt=12&ls=1

Credit to Yann Ramin for his advice. I think there is a better solution with links, but this was easy and fast.

Good luck!

nevieandphil
  • 390
  • 4
  • 11
  • Are you referring to just normal Xcode? Or is there some special version called "XCode: development tools"? Because I only see normal Xcode in the App Store. Granted, this is 4 years into the future from when this answer was posted. – user124384 Apr 22 '17 at 15:36