C++11 on Mac with Clang or GCC

2019-04-04 03:56发布

I have Xcode 4.5.2 on Moutain Lion, and I have install the lastest "Command Line Tools" but when I tried to compile with g++ or clang++ (and the options -std=c++11 -stdlib=libc++) I get an error. With g++:

cc1plus: error: unrecognized command line option "-std=c++11"
cc1plus: error: unrecognized command line option "-stdlib=libc++" 

With clang++:

clang: error: invalid deployment target for -stdlib=libc++ (requires OS X 10.7 or later)

It's in a Qt project.

So how can I used the C++11 on my Mac ?

3条回答
我只想做你的唯一
2楼-- · 2019-04-04 04:13

Try -std=c++0x if c++11 doesn't work. Support for the -std=c++11 option is relatively new in GCC and you might not have a recent enough version.

I'd trust Marshall on the libc++ issue.

查看更多
孤傲高冷的网名
3楼-- · 2019-04-04 04:32

I installed gcc-4.7 on my Mac to make C++11 work. GCC in its current version is fairly good at supporting C++11, so this should be a fair choice.

The installation can be done by Homebrew and is not that complicated (at least I was able to do it...)

To install Homebrew if you do not already have it:

ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"

Now run

brew doctor

and fix whatever problems come up (there is something written in the hombrew documentation for that). Finally, install current gcc:

brew install gcc

If everything goes well you should be able to access g++-4.7, which allows -std=c++0x.

查看更多
We Are One
4楼-- · 2019-04-04 04:33

As you found, g++ does not support those command line options.

It sounds like you're using Xcode.

For clang, you should look at the project settings, and make sure that the "Deployment Target" is set to 10.7 (or 10.8)

What the error message is telling you is that libc++ is not available for 10.6 and before.

查看更多
登录 后发表回答