Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode compiler cannot find C++ 11 includes

Tags:

xcode

c++11

I developed some classes in Visual Studio 2012 which make heavy use of C++11 features and also uses new std features like threads and mutex. The reason was to be platform independent. Trying to port the code to OSX the Xcode compiler 5.0 cannot open following includes:

#include <chrono>
#include <thread>
#include <functional>
#include <mutex> 

I already switch the build settings to C++11 and searched the usr/include path for the includes. Please do not suggest boost as I try to eliminate it from my projects.

like image 760
Martin Schlott Avatar asked Dec 15 '22 05:12

Martin Schlott


2 Answers

You need both -std=c++11 and -stdlib=libc++:

$ cat cpptest.cpp
#include <chrono>
#include <thread>
#include <functional>
#include <mutex>

int main() {
    return 0;
}

$ clang -o cpptest -std=c++11 -stdlib=libc++ cpptest.cpp
$ echo $?
0
like image 77
trojanfoe Avatar answered Jun 03 '23 12:06

trojanfoe


Give that you're using Xcode, you shouldn't need to specify explicit flags to clang or add build paths to /usr/include, as that is done for you by the IDE. What you need to do is open the project settings -> Build Settings and set C++ Language Dialect to C++11 and the C++ Standard Library to libc++ (LLVM C++ standard library with C++11 support)

like image 40
the_mandrill Avatar answered Jun 03 '23 13:06

the_mandrill