0

I'm new to Mac. I have some Linux and Windows C++11 source which uses Boost I'd like to build on this Mac. Installed MacPort (should I instead be using Homebrew?) then successfully ran commands such as:

sudo port install cmake
sudo port install boost
sudo port install openssl
sudo port install gcc49
sudo port install gcc_select
sudo port install --set gcc mp-gcc49

CMake correctly finds Boost 1.57.0 and sets up the makefile. But when I run make, it seems it cannot find normal C++11 headers such as "chrono":

In file included from ../src/test.cpp:10:
../src/test_private.hpp:33:10: fatal error: 'chrono' file not found
#include <chrono>
         ^
1 error generated.

Indeed, when I go looking for the C++ header files, I see some of them in /usr/include/c++/4.2.1/ but newer files such as chrono and thread are missing.

Is there another package I need to install before I can compile C++11 source code on a Mac?

Stéphane
  • 19,459
  • 24
  • 95
  • 136
  • I'm 95% sure this is a duplicate of http://stackoverflow.com/questions/14656856/getting-c11-compliant-compiler/14657009#14657009 – Bill Lynch Mar 20 '15 at 21:12
  • @BillLynch No, I'm already passing in the flag, and passing it in doesn't install the missing header files. – Stéphane Mar 20 '15 at 21:18
  • Could you show us the command that you are using to try and compile `src/test.cpp`? – Bill Lynch Mar 20 '15 at 21:41
  • @BillLynch was right. The real C++11 headers are installed in `/usr/lib/c++/v1/` instead of where I was looking in `/usr/include/c++/`. To get things to compile correctly, I had to add `-stdlib=libc++` to the compiler flags, not `-std=gnu++11` nor `-std=c++11`. – Stéphane Mar 20 '15 at 21:56
  • Upvoted to counter the moron's down vote. – Howard Hinnant Mar 20 '15 at 21:59
  • If you're on >= 10.9 and are trying to use libraries from MacPorts, save yourself some time and compile with `clang++`. MacPorts' g++ will use libstdc++, but all libs in MacPorts use clang's libc++, so you'll end up mixing standard libraries. At best, it won't link, at worst, it'll crash at runtime and you'll have no idea why. – neverpanic Mar 21 '15 at 17:40

1 Answers1

2

Xcode, the Apple supplied compiler/tools, comes with two implementations of the std::lib:

  1. gcc's libstdc++, version 4.2.
  2. libc++

The first is very, very old, and does not support anything in C++11 such as <chrono>. The second supports C++11 quite well, but can only be used with clang, not gcc. clang comes with Xcode.

You will also need to install command line tools after you install Xcode:

xcode-select --install
Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
  • Trying to run `sudo xcode-select --install` results in the following error message: `xcode-select: Error: unknown command option '--install'.` – Stéphane Mar 20 '15 at 21:39
  • Sorry, perhaps there is info here that will help: http://stackoverflow.com/questions/9329243/xcode-4-4-and-later-install-command-line-tools – Howard Hinnant Mar 20 '15 at 21:57