How to Use C++11 with Xcode

Can I use C++11 with Xcode?

Xcode 4.2 had finally added support for C++0X:

  1. In the project build settings screen, switch on "All" options.

  2. In the "Build Options" section, set compiler to "Apple LLVM compiler 3.0".

  3. Scroll down to "Apple LLVM Compiler 3.0 - Language" section and set "C++ Language Dialect" to "C++0X" and "C++ Standard Library" to "libc++".

The std::move(), move constructor and R-Value reference are known to work as expected, and I'm testing on the std::thread and std::atomic.

How to compile a project with C++11 under Xcode 8?

It's the same for 7 and 8. From the Project Navigator, select the project. In the main panel, at the top left, select the target. Now the main panel should have General, Resource Tags, Build Settings, Build Phases, & Build Rules along the top. Select Build Settings, select All. Scroll down to "Apple LLVM 8.0 Language C++" and expand it. Change "C++ Language Dialect" to "C++11 [-std=c++11]".

I'm having some trouble with C++11 in Xcode

I use Xcode and set the following settings:

C++ language dialect: C++11 or GNU++11

C++ Standart Library: libc++ (LLVM C++ standart library with C++11 support)

Xcode version: 4.3.2

XCode project settings

How to compile C++ with C++11 support in Mac Terminal

As others have pointed out you should use clang++ rather than g++. Also, you should use the libc++ library instead of the default libstdc++; The included version of libstdc++ is quite old and therefore does not include C++11 library features.

clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp

If you haven't installed the command line tools for Xcode you can run the compiler and other tools without doing that by using the xcrun tool.

xcrun clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp

Also if there's a particular warning you want to disable you can pass additional flags to the compiler to do so. At the end of the warning messages it shows you the most specific flag that would enable the warning. To disable that warning you prepend no- to the warning name.

For example you probably don't want the c++98 compatibility warnings. At the end of those warnings it shows the flag -Wc++98-compat and to disable them you pass -Wno-c++98-compat.

Compile C++11 code on mac?

Outside of an IDE (e.g., in shell), I normally have the variable CXX set to: "clang -std=c++11 -stdlib=libc++" in .profile / .tcshrc / etc., since this is picked up by most configure scripts too. On the cmd line I might use: $CXX -c foo.cc

MacPorts gcc-4.8.1 works well: "[sudo] port install gcc48 [-universal]"

"[sudo] port select --set gcc gcc48" will make this the default gcc, g++, etc.

Don't attempt to update or modify the system tools, like the old gcc-4.2 / llvm hybrid that comes with Xcode.

I don't know what you mean by 'best' way in the 3rd part of your question, but with Apple's support (they employ the primary author of LLVM), and other projects like FreeBSD behind it, clang will only continue to improve. It's already much faster than gcc, has far better error messages / diagnostics (especially for C++ and templates), and a modular architecture. For OS X, it's the clear choice.

Xcode compiler cannot find C++ 11 includes

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

Xcode 4.3 and C++11 include paths

You need:

-std=c++0x

to select C++11. And you need:

-stdlib=libc++

to select libc++. By default, the std::lib that shipped with gcc 4.2 is used, which is pre-C++11.



Related Topics



Leave a reply



Submit