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

I wanted to compile C ++ 11 source code in 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. The terminal is always read unrecognized command line option . However, g++ -std=gnu and such things worked fine (of course, the C ++ 11 source code could not get through).

What option should I use to enable C ++ 11 support?

By the way, the command line tool that I'm using is installed in Xcode, and I'm sure they are updated.

+51
c ++ terminal c ++ 11 g ++ macos
Jan 09 '13 at 5:34 on
source share
2 answers

As others have pointed out, you should use clang++ , not g++ . In addition, you should use the libC ++ library instead of the standard libstdC ++; The included version of libstdC ++ is quite old and therefore does not include the capabilities of the C ++ 11 library.

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

If you have not installed the command line tools for Xcode, you can start the compiler and other tools without doing this using the xcrun tool.

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

Also, if there is a special warning that you want to disable, you can pass additional compilers for this. At the end of the warning messages, the most specific flag that will include the warning is displayed. To disable this warning, you add no- to the warning name.

For example, you probably do not need C ++ 98 compatibility warnings. At the end of these warnings, it displays the -Wc++98-compat and disables them, you pass -Wno-c++98-compat .

+77
Jan 09 '13 at 5:52
source share

Xcode uses clang and clang++ when compiling, not g++ (unless you have configured any things). Try instead:

 $ cat t.cpp #include <iostream> int main() { int* p = nullptr; std::cout << p << std::endl; } $ clang++ -std=c++11 -stdlib=libc++ t.cpp $ ./a.out 0x0 

Thanks to bames53 for pointing out that I left -stdlib=libc++ .

If you want to use some GNU extensions (as well as use C ++ 11), you can use -std=gnu++11 instead of -std=c++11 , which will enable C ++ 11 mode as well as enable GNU extensions.

+20
Jan 09
source share



All Articles