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?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 2
    You need to install xcode. – user657267 Mar 30 '15 at 22:50
  • What happens if you try searching outside your clang folder for the header? For example, /Library or /System/Library? – Alex Reynolds Mar 30 '15 at 23:29
  • `find / -name unistd.h` finds files only in `/Applications/Xcode/*`, and I tried to get by without using Xcode. –  Mar 31 '15 at 08:09
  • 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 ... – Rob Apr 22 '16 at 16:02

3 Answers3

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.

user657267
  • 20,568
  • 5
  • 58
  • 77
  • 1
    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! –  Mar 31 '15 at 08:04
  • 2
    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) – such Jan 24 '16 at 11:33
  • @user657267 `brew install unistd` responds with unistd not found - which specific homebrew package do you suggest I install? – Brad Hein Apr 06 '19 at 14:25
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.)

Navin
  • 3,681
  • 3
  • 28
  • 52
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!

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 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? –  Mar 30 '15 at 23:25
  • 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. – user657267 Mar 31 '15 at 00:27
  • @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.... – Lightness Races in Orbit Mar 31 '15 at 00:28