Qt 5.0 and c++11 with OSX Mountain Lion

2019-02-25 10:17发布

问题:

I'm giving c++ a try again after being away for 7 years. I've downloaded the newly released Qt 5.0 sdk for osx, but I can't get a basic c++11 feature compiled using Qt Creator. The following statement:

auto i = 3;

results in a compilation error:

mainwindow.cpp:19: error: ISO C++ forbids declaration of 'i' with no type

I've google around for similar problems and found suggestions to put

QMAKE_CXXFLAGS += -std=c++11
  or
CONFIG += c++11

in the .pro file. Unfortunately without success .The build fails on unrecognized command line options.

I must be doing something wrong. Any suggestions?

Thanks,

Frans

回答1:

It's looks like Qt Creator for Mac bug. I fixed it using

QMAKE_CXXFLAGS += -std=c++11 -stdlib=libc++ -mmacosx-version-min=10.7
LIBS += -stdlib=libc++ -mmacosx-version-min=10.7

in .pro file



回答2:

I don't know if this is still an issue for you.

On my current system (Max OS X Yosemite - Qt 5.4 - QtCreator 3.0 - XCode 6.1.1) this works like a charm:

In .pro file:

macx {
    QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.7
    QMAKE_CXXFLAGS += -mmacosx-version-min=10.7
    QMAKE_CFLAGS += -mmacosx-version-min=10.7
    QMAKE_MAC_SDK = macosx10.9
}

macx {
    QMAKE_CXXFLAGS += -std=c++11 -stdlib=libc++
    QMAKE_CFLAGS += -std=c++11 -stdlib=libc++
}
CONFIG += c++11

Make sure you have the latest Mac OS X SDK installed too.



回答3:

According to the gcc feature list, auto is supported since gcc 4.4through:

g++ -std=c++0x

you could also try the gcc-specific form:

g++ -std=gnu++0x

Maybe, in your case, an additional problem occurs (g++ parameters not correctly promoted to the compiler).