Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Program C++11 Using Qt5?

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

like image 508
IAE Avatar asked Jun 16 '12 18:06

IAE


People also ask

Does Qt support C ++ 11?

Qt Creator is not a compiler. When you read that "Qt Creator supports C++11" it means that the code-completion engine (Clang in this case) supports C++11 syntax.

Does Qt Creator support C?

In addition, the Qt Creator Bare Metal Device plugin provides support for the following compilers: IAREW is a group of C and C++ bare-metal compilers from the various IAR Embedded Workbench development environments. Note: Currently supported architectures are 8051 , AVR , ARM , STM8 , and MSP430 .

Does Qt support C++?

Supported LanguagesQt's C++ APIs provide features for easy cross-platform development. Design UIs with Qt QML - a declarative language with the ability to describe business logic with JavaScript. If you prefer Python or other languages we have a wide range of language bindings.

Does Qt support C ++ 17?

As mentioned, Qt 6 makes C++17 a requirement in order to adopt its more advanced language features.


1 Answers

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.

like image 192
Oipo Avatar answered Oct 02 '22 20:10

Oipo