Pierre_colin
Pierre_colin

Reputation: 123

How to use C++11 in Qt creator 4.8

I have looked everywhere and the solution to this is to add a line of code to a '.pro' file in my project. I cannot see any such files though! I can see a qbs file, the main.cpp file and a xcode.qbs file.

This is how I started: using a Mac, I installed Qt Creator 4.8 (Clang v5.12) and I created a 'plain C++ project' which uses Qmake.

I am using the {} to initialise a variable but it won't allow me to. I had a similar problem with Eclipse and found a solution. Any help, please?

This is the code I am testing:

#include <iostream>

int main()
 {
   int a{5};
   std::cout << a;
   return 0;
  }

The error code for the line where I define a is: "/Users/xxxxxx/Coding/HelloEarth/main.cpp:5: error: expected ';' at end of declaration"

What should I do?

Upvotes: 1

Views: 1237

Answers (1)

Constantinos Glynos
Constantinos Glynos

Reputation: 3186

This is the simplest approach to creating a qt project:

  1. Open a new terminal and create an empty folder Foo/ in your Desktop: ~$ cd Desktop; mkdir Foo/
  2. Change to that folder: ~$ cd ~/Desktop/Foo
  3. Create 2 files: ~$ touch Foo.pro main.cpp
  4. If you have exported the correct paths in your .profile or .bashrc, then you can type ~$ qtcreator in your terminal to run qt. Otherwise, click on the QtCreator App from the applications.
  5. Go to File -> Open project and select the Foo.pro file.
  6. Keep the default paths to debug and release mode, and hit continue, or ok (can't remember exactly)
  7. You should have the Foo.pro file open in Qt. Now enter the following lines in the Foo.pro file and hit save (Ctrl+S).
CONFIG  +=  c++11 console
TARGET  =  My_Foo
SOURCES +=  main.cpp
  1. Once you hit save, you should see the main.cpp file on the left pane that lists the project files. If so, click on the Projects button (spanner tool) on the left menu.
  2. Under the Build option, untick shadow build and under the Run option, untick Run in terminal.
  3. Go back to your main.cpp file and type a simple hello program. Press Ctr+S (to save), Ctrl+B (to build the project) and Ctrl+R (to run).

Up until step 7, you can do it using the GUI, by selecting Create new Project and then select console application. Btw, on mac use Cmd instead of Ctrl.

Also make sure you have the correct compilers setup. (https://doc.qt.io/qtcreator/creator-tool-chains.html)

Upvotes: 1

Related Questions