16

I'm getting ready to program a cross-platform project with my friend. We decided on using Qt and gcc as our IDE and toolchain respectively. He works on Linux, I work on Windows.

However, gcc on Linux isn't necessarily gcc on Windows. More specifically, Qt on Windows installs mingw with gcc 4.4 and my friend has gcc 4.7. So I tried getting a more recent gcc version for windows.

My current version of Qt is 5 and using gcc 4.7 downloaded and installed from this site: http://www.equation.com/servlet/equation.cmd?fa=fortran

I installed it in C:\QtSDK\mingw and simply overrode all the files existing from the Qt installation. I figured that I wouldn't have to reconfigure anything in Qt if I just took the short route.

However, even using the compiler flags:

QMAKE_CXXFLAGS += -std=c++0x (Qt 4.7)

and

CONFIG   += c++11 (Qt5)

the IDE or toolchain fails to compile a simple range-based for loop:

int main(int argc, char *argv[])
{
    int my_array[5] = {1, 2, 3, 4, 5};
    for (auto x : my_array)
        std::cout << x << std::endl;

    return 0;
}

or initializer-lists:

int main(int argc, char *argv[])
{
    QVector<int> testsData { 1, 2, 10, 42, 50, 123  };
    QStringList options = { QLatin1String("foo"), QLatin1String("bar")  };

    return 0;
}

However, looking at the implementation details of gcc 4.7, both of these features- and more- should be readily available.

Has anyone else tried to use gcc and Qt for Windows? If so, how did you get it to work? I would like a solution using gcc 4.6 or 4.7, but will settle for less if it is not at all possible.

Alternatively, is there a dev environment for Linux and Windows that makes use of C++11 features? I would also settle for something besides Qt if it just works...

I used the sources:

C++0x in Qt (Qt 4.7-4.8)

C++11 in Qt5

IAE
  • 2,213
  • 13
  • 37
  • 71
  • Note: using `QMAKE_CXXFLAGS += -std=c++0x` in Qt5 results in the error "D9002: Ignoring unknown option -std=c++0x". Apparently, this switch has been removed in the new Qt version. – IAE Jun 16 '12 at 18:39
  • Sorry, I'm just writing off what the About button says: Qt Creator 2.5 Based on Qt4.8 (32bit) Built on May 9th 2012 (Release day of Qt5). Is any of this information I'm using wrong? – IAE Jun 17 '12 at 06:59
  • 2
    Yes: Qt Creator is not Qt. And Qt Creator 2.5 is not "Qt5". Qt Creator is an IDE, Qt is an application framework. – rubenvb Jun 17 '12 at 09:23
  • Thank you for clearing up my misunderstanding :) – IAE Jun 17 '12 at 09:56
  • you're welcome. It's a (way too) common misconception. It happens a lot, and it triggers my not so nice side `:)` – rubenvb Jun 17 '12 at 10:00
  • What's the benefit of using `QLatin1String` over string literals here, when the target is a `QString` anyway? – Johannes Schaub - litb Dec 09 '12 at 13:20
  • @JohannesSchaub-litb: The `QString` constructor taking a string literal is not available if you compile with `QT_NO_CAST_FROM_ASCII` being defined (see [here](https://qt.gitorious.org/qt/qt/blobs/c7e34fccc4366391487d6d9eb4bb58dd374e8035/src/corelib/tools/qstring.h#line418)). This is generally a good idea to avoid introducing subtle encoding bugs (you probably also want to define `QT_NO_CAST_TO_ASCII`). – Frerich Raabe Jan 01 '13 at 22:49

2 Answers2

13
QMAKE_CXXFLAGS += -std=c++0x

Results in an error because you're not using the MinGW compiler. "D9002: Ignoring unknown option -std=c++0x" is an error from the MSVC compiler.

In QtCreator, go to the projects tab(on the left) and change the toolchain you're using. If it hasn't auto-detected MinGW, you're going to have to add it yourself by clicking on the manage button.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Oipo
  • 158
  • 2
  • 8
  • Hey! Thanks for the answer, but I have gone to Tools -> Options -> Build & Run -> Unconfigured Project to change the toolchain. I even created a second mingw location C:/QtSDK/ming4.8 and created a separate entry for this new toolchain. Neither mingw toolchains work and both give me the same warning. Am I perhaps doing something wrong? – IAE Jun 16 '12 at 18:54
  • Does that mean you fixed it? Tools -> Options is not per-project configuration though. – Oipo Jun 16 '12 at 19:11
  • Yes. I didn't realize that Tools->Options didn't actually change the build configuration. That is... gyah! Anyway, it works now, thanks a lot :) – IAE Jun 17 '12 at 06:57
  • 1
    Just a note for future readers: using GCC/MinGW >= 4.7 you'd better use flag `-std=c++11` and thus get more sweet features such as lambdas in `connect`. Also, it seems to me that in Qt 5.0.1 `CONFIG += c++11` is not needed in .pro file (and doesn't compile). – NIA Feb 10 '13 at 12:14
10

UPDATE:

According to this link: https://wiki.qt.io/How_to_use_C%2B%2B11_in_your_Qt_Projects

It is now possible to just do this:

CONFIG += c++11

or

CONFIG += c++14

Also the upcomming Qt 5.7 WILL REQUIRE C++11 compiler by default and will drop support for older non-C++11 compiler.

Older UPDATE:

To enable C++11 - GCC 4.7 and newer is required:

qmake build system:

QMAKE_CXXFLAGS += -std=c++11

qbs build system:

cpp.cxxLanguageVersion: "c++11"

To enable C++14 - GCC 4.9 and newer is required:

qmake build system:

QMAKE_CXXFLAGS += -std=c++14

qbs build system:

cpp.cxxLanguageVersion: "c++14"

NOTE: Please note that "c++11", "c++14" etc. are case sensitive.


Obsolete version:

qbs build system:

cpp.cxxFlags: "-std=c++11"
cpp.cxxFlags: "-std=c++14"

Older version:

GCC 4.7 is now recommended for Qt5. With GCC 4.7 you can use QMAKE_CXXFLAGS += -std=c++11

Edit: With GCC 4.9 it is now possible to enable C++14 by passing this flag: QMAKE_CXXFLAGS += -std=c+11

Zingam
  • 4,498
  • 6
  • 28
  • 48