clang++: fatal error: 'unistd.h' file not found

20,795

Solution 1

Clang does not include a c library or system headers, you'll need to install Xcode if you want to program on OS X. This page has some more details if you don't want to install the entire Xcode package, you can get away with the command line tools only.

Once you have Xcode / command line tools installed you can either use the versions of clang and gcc included with Xcode or the one you downloaded, although it's usually easier to use Macports or Homebrew if you're looking to use up to date versions of either.

Solution 2

That error disappeared after running xcode-select --install.

(I only keep HomeBrew's LLVM and clang regularly updated, so my original macos headers must have been ancient/non-existent until I used the above command to fix that.)

Share:
20,795
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    Using OS X 10.10.2, I download Clang for Mac OS X v. 3.6.0, and try to compile a simple Hello World program.

    The commands I use are these:
    (assumes you downloaded clang to .)

    cd .
    ./clang+llvm-3.6.0-x86_64-apple-darwin/bin/clang++ main.cpp
    

    The result is this:

    In file included from main.cpp:1:
    In file included from ./clang+llvm-3.6.0-x86_64-apple-darwin/bin/../include/c++/v1/iostream:37:
    ./clang+llvm-3.6.0-x86_64-apple-darwin/bin/../include/c++/v1/__config:23:10: fatal error: 'unistd.h' file not
      found
    

    which makes sense, as there is no file unistd.h, as verifiable by

    find . -name unistd.h
    

    which yields 0 results.

    I tried downloading the LibC++ source code v. 3.6.0 but even that contains no unistd.h. Where can I find the official/vanilla version of that file, and the files that are referenced, and meta-referenced, by it?

  • Admin
    Admin about 9 years
    Am I understanding things correctly if every OS has to provide its own unistd.h? Is this why I ‘need’ the Apple version? The whole point of this endeavor was to minimize the Xcode dependency… Is there no generic unistd.h for UNIX-based systems?
  • user657267
    user657267 about 9 years
    The Xcode headers are fine with clang, in fact it's the only way to get system / c headers on OS X (barring copying them from apple's source page), OP will need Xcode to do just about anything unless he doesn't use any headers whatsoever. Manually installing clang on OS X is also usually a waste of time given the existence of macports and / or brew.
  • Lightness Races in Orbit
    Lightness Races in Orbit about 9 years
    @user657267: Alright great you'd best get started on an answer then! ATM you seem to just be spreading your answer across comments, which are not for that....
  • Admin
    Admin about 9 years
    I can use the -I option in any clang version and point to the Xcode include files, e.g. clang++ -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOS‌​X.platform/Developer‌​/SDKs/MacOSX10.10.sd‌​k/usr/include main.cpp. Thanks!
  • such
    such over 8 years
    The command line tools for Xcode can be installed by running xcode-select --install (and you'll need to reinstall them whenever Xcode is updated)
  • Brad Hein
    Brad Hein about 5 years
    @user657267 brew install unistd responds with unistd not found - which specific homebrew package do you suggest I install?