Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I wanted to compile C++11 source code within Mac Terminal but failed. I tried g++ -std=c++11, g++ -std=c++0x, g++ -std=gnu++11 and g++ -std=gnu++0x but nothing worked. Terminal always read unrecognized command line option. However, g++ -std=gnu and things like that worked fine (of course C++11 source code could not pass).

Which option should I use to turn on C++11 support?

By the way, the command line tool I'm using is installed within Xcode, and I'm pretty sure that they are up-to-date.

like image 801
4ae1e1 Avatar asked Jan 09 '13 05:01

4ae1e1


People also ask

Can you compile C on Mac?

C code can be written in any platform like Mac, Windows, etc. C compilers compile C code and create an executable according to the platform. The executable created for one platform can only be executed on that platform.

How do I enable C++ 11 on my Mac?

Go to property of the project --> c/c++ build --> settings --> cross gcc compiler and cross g++ compiler. Put -std=c++11 under miscellaneous part of both tabs. Click Apply.


1 Answers

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.

like image 58
bames53 Avatar answered Sep 25 '22 19:09

bames53