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

3

14

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?

Nichellenichol answered 30/3, 2015 at 22:42 Comment(4)
You need to install xcode.Maurili
What happens if you try searching outside your clang folder for the header? For example, /Library or /System/Library?Cotangent
find / -name unistd.h finds files only in /Applications/Xcode/*, and I tried to get by without using Xcode.Nichellenichol
I got this error trying to 'brew upgrade postgresql' on OSX 10.11.4 El Capitan. For me, it looks like my xcode had gotten an year out of date ...Fauman
M
7

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.

Maurili answered 31/3, 2015 at 0:38 Comment(3)
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/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include main.cpp. Thanks!Nichellenichol
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)Orts
@Maurili brew install unistd responds with unistd not found - which specific homebrew package do you suggest I install?Fillip
R
3

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

Rendon answered 30/8, 2017 at 18:3 Comment(0)
K
0

unistd.h is not a C or a C++ header! It is a POSIX header.

You could install XCode as it'll come from that, but I honestly don't know whether that'll be at all compatible with the Clang runtime. I'm not a Mac person.

Perhaps you can find an alternative; for example, there's no way you need unistd.h to compile a "Hello world" program!

Korman answered 30/3, 2015 at 23:8 Comment(3)
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?Nichellenichol
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.Maurili
@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....Korman

© 2022 - 2024 — McMap. All rights reserved.