18

I'm trying to compile an application that utilizes cstdint. Since Apple deprecated the gcc, I wanted to try compiling it with clang, but i get the error:

fatal error: 'cstdint' file not found

I know that the gcc 4.6.2 version has the option for -std=c++0x, to include the library, but since the os x version is 4.2 that's not much of an option here. Any suggestions on how I can move forward? I have tried to install 4.6.2, but ran into a variety of issues when compiling some of the needed libraries before building gcc.

agmcleod
  • 13,321
  • 13
  • 57
  • 96
  • I was able to get GCC 4.7.0 compiled up on Mac OS X 10.7.3 without much hassle. I believe I used the Apple GCC to bootstrap the compilation, rather than my own 4.6.1. Contact me if you want help (see my profile). (I was unsuccessful with 4.6.2 and 4.6.3.) – Jonathan Leffler Apr 12 '12 at 03:22

1 Answers1

18

Presumably, you have the source code to this application, so you can modify the headers to include the correct cstdint header, as Clang 3.0 (which Lion's tools come with) does have the header.

Quick Solution

The header is under the tr1 directory, so you will want to do either of these includes:

#include <tr1/cstdint>

Or

#include <stdint.h> // which will use the C99 header

Longer, boring explanation

After doing some additional reading since I remember you can do this without the tr1 directory:

By default, you are going to be including C++ headers from /usr/include/c++/4.2.1, which are the GNU GCC headers. /usr/include/c++/4.2.1/tr1 includes the TR1 header files, like cstdint.

The alternative method is to compile using the Clang++ frontend and passing the -stdlib=libc++ flag, which will use the headers from /usr/include/c++/v1, which are Clang's C++ header implementations. It has cstdint.

Example:

// file called example.cxx
#include <tr1/cstdint>

int main() {
    // whatever...
}

Compile this with:

g++ example.cxx

or

clang++ example.cxx

And it will do what you want.

If you don't want to use the tr1 version (which is roughly the same, if not exactly):

// file called example.cxx
#include <cstdint>

int main() {
    // stuff
}

This is compiled like this:

clang++ -stdlib=libc++ example.cxx

Though if you use -stdlib=libc++, it means you're linking to Clang's C++ library libc++, rather than GCC's libstdc++.

wkl
  • 77,184
  • 16
  • 165
  • 176
  • Clang doesn't use GCC's C++11 implementation with its `-std=c++11` flag. Maybe the bundled GCC stdlib is old. But of course GCC has ``. – Potatoswatter Oct 15 '12 at 01:25
  • @Potatoswatter - the bundled GCC libs on Mac OSX are frozen at 4.1.2 because of the licensing changes between that and the subsequent versions, so yeah, it's a little old. If you want better C++11 support on Mac your options are either to install a newer GCC or use the Clang implementation. – wkl Oct 15 '12 at 01:38
  • 2
    I got this error on Ubuntu 14.04, I had to install `libc++-dev` to get the development files. Then `-stdlib=libc++` worked fine. – nathancahill Dec 14 '15 at 22:31