10

On OSX, I can't compile simple programs from the command-line when I use an externally-provided compiler (e.g. gcc, or a custom clang install). System headers like limits.h cannot be found.

Example error messages:

fatal error: limits.h: No such file or directory

Or:

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

Or:

fatal error: 'stdlib.h' file not found

and so on.

What's wrong?

Stuart Berg
  • 17,026
  • 12
  • 67
  • 99

3 Answers3

22

Make sure you've installed the xcode command-line tools:

xcode-select --install

(Accept the pop-up dialog.)

That will install system headers into standard locations expected by tools like gcc, e.g. /usr/include.

Stuart Berg
  • 17,026
  • 12
  • 67
  • 99
  • gcc should have installed its own set of header files, which it knows how to find. Using header files from one compiler (clang, from Xcode) with another compiler (gcc, installed separately), is quite probably asking for trouble. – Greg Hewgill Jun 16 '15 at 20:40
  • 2
    gcc installs its own C++ headers, but for C headers the story is different. It attempts to use the system's own C-headers. On some (ok, many) systems, the C-headers are not ANSI-compliant, so gcc will provide its own "fixed" headers to replace a few of the "broken" system headers. But for those headers that did not need to be "fixed", there will be no gcc copy of it. Only the original system header will be used. For details, see the documentation for the gcc "fix-includes" step when gcc is compiled from source. – Stuart Berg Jun 16 '15 at 20:52
  • I found this errors when trying to install manually some individual packages. I am running python 2.7 with conda as environment manager, and have gcc 5.0 installed all within mac os x 10.12.3 Sierra. As far as i remember my gcc and command line tools were perfectly installed and running. Anyway, reinstalling them, solve the issue. – Hugo Mar 10 '17 at 20:30
2

If this doesn't work, it may have to do with the upgrade to macOS 10.14 Mojave

See the answer here for how to resolve:

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

user2688151
  • 167
  • 2
  • 9
0

somehow when you install mojave it deletes headers- run:

open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
Sunny
  • 5,825
  • 2
  • 31
  • 41