Tuesday, September 10, 2013

How to Setup 64-bit Qt 5 to use with NetBeans 7.4, 8.0 or Higher on Windows

Updated: 04/18/2016

I will show you how to setup 64-bit Qt 5 on Windows to use with NetBeans IDE 7.4, 8.0 or higher.

Oracle JDK 8 update 20 and NetBeans IDE 7.4, 8.0 or higher is required. I will not go over installing these as it is clearly documented.

Next you will need to download the MSYS2 installer here. Make sure you download the 64-bit installer. It is good to have MSYS2 because it makes it easy to install and update MinGW-w64, GNU Make and other tools to build on Windows. Since as of this writing, there are no 64-bit Qt 5 Windows builds on the qt-project.org website, but we can install the MSYS2's 64-bit Qt 5 package.

Optionally, you can download Rapid Environment Editor here. It allows easier editing of system environment variables on Windows.

Installing MSYS2, MinGW-w64 & Qt-x64

  • When creating folders for installation, do not use folder names with spaces or deeply nested folder hierarchies.

  • Run the MSYS2 installer and install it to a convenient place on your system drive. I will use C:\Developer\msys64\ for these instructions. Navigate to the MSYS2 base directory and run msys2_shell.bat. An MSYS2 shell window will open and you will see some setup activity going on. Once the setup activity is done, you must close the shell window and run is again. Now run update-core. If one of the packages is updated during script run you must restart MSYS2. Keep running update-core and restarting MSYS2 until update-core cannot be found. At this point you have been updated to pacman 5.0.1.6403 and update-core is obsolete. To update everything now, you simply keep running pacman -Syuu and follow the instructions. Repeat this step until it says there are no packages to update.

  • Now you need to install the build packages. The first one is pacman -S base-devel. Then install pacman -S mingw-w64-x86_64-toolchain for building mingw64 packages. Optionally you can install pacman -S mingw-w64-i686-toolchain for building mingw32 packages.

  • Next Install the 64-bit Qt 5 packages using MSYS2's pacman by running pacman -S mingw-w64-x86_64-qt5. Then run pacman -S mingw-w64-x86_64-qt-creator to install the QT Creator related packages. Optionally you can install the 32-bit packages if you have installed the MinGW-w32 packages. i.e. pacman -S mingw-w64-i686-qt5 mingw-w64-i686-qt-creator.

  • See the MSYS2 Installation instructions for more information about updating the core packages, updating other packages and using the package manager.

  • See this page for more information about installing and using MSYS2 build tools.

  • Next you need to add the MSYS2 and MinGW-w64 bin folder paths to the Windows system path. To do this in Windows 7, go to the Start Menu and search "advanced system" and click on "View Advanced System Settings". Then click on the "Environment Variables" button.

Windows 7 Environment Variable

  • Now you can enter the folder paths directly in the system path, but I like to create two variables MINGW_HOME and MSYS_HOME for these paths (omitting the bin folder) and then adding those variables to the path string. In our case MINGW_HOME will be C:\Developer\msys64\mingw64 and MSYS_HOME is C:\Developer\msys64\usr. Do not add a backslash at the end!

Windows 7 Edit System Variable

  • Next, edit the PATH variable and add %MINGW_HOME%\bin;%MSYS_HOME%\bin at the end.

Windows 7 Edit System Path

  • Check to see if the MSYS2 and MinGW-w64 paths are valid by entering path in the Windows command prompt interpreter cmd.exe. Do not include any other paths to build tools of the same name before these new entries or your builds will fail!

Windows 7 cmd.exe

  • Check to see if qmake.exe, g++.exe and make.exe is accessible by typing qmake --version, gcc --version and make --version in the Windows command prompt from a folder outside of where they are located. You may continue to do the same for the rest of the tools gcc.exe, gfortan.exe, as.exe and gdb.exe.

Setting up the Build Tools in NetBeans

  • Now you should already have NetBeans installed and have installed the full package—in which you can just activate C/C++ through Options—or you will have to install the C/C++ plugin with the NetBeans plugin manager.

  • Go to Tools >> Options >> C++ and you will see the build tools panel. If NetBeans has not already found the standard build tools automatically, try clicking the "Restore Defaults" button. If this does not work, you will need to add them manually. To add the build tools, click the "Add..." button and then add the path to the bin folder—in which you can just activate C/C++ through Options—or you will have to install the C/C++ plugin with the NetBeans plugin manager.

  • Go to Tools >> Options >> C++ and you will see the build tools panel. If NetBeans has not already found the build tools automatically, you will need to add them manually. To add the build tools, click the "Add..." button and then add the path to the bin folder.

NetBeans IDE 8.0.1 Add New Tool Collection

  • You will now see a tool collection in the Build Tools panel.

NetBeans IDE 8.0.1 C/C++ Build Tools

  • To test this, we will create a sample Qt project. Go to File >> New Project >> Categories: Samples: C/C++, Project: Hello Qt World. Click the Next button and accept all the default settings by clicking the Finish button.

NetBeans IDE 8.0.1 New C/C++ Application

  • You will now see the new project in the NetBeans Project Window.

  • Next, open the Project Properties and under Categories: Run, change the Console Type to either "Standard Output" or "External Terminal", otherwise the project will fail to build. (The "Internal Terminal" option will only work if you have Cygwin installed.)

NetBeans IDE 8.0.1 C/C++ Project Properties

  • Press F11 on your keyboard to build the project. The project should have built successfully and you will see a "successful" message in the NetBeans output window.

NetBeans IDE 8.0.1 C++ Build Output Window

  • Press F6 on your keyboard to run the project. You should see a "Hello Qt World" dialog box come up with a field prompting you to enter your name. Enter something and then click the red x in the upper right hand corner of the dialog box to close the program. You will then see a "run successful" message in the NetBeans output windows.

Qt 5.3.1 Hello World Sample Program

  • At this point, you may want to look at some of the files in the project. If you see a little question mark on the project's Qt icon in the Projects window and error badges on the files, right click on the project name and choose Code Assistant >> Reparse Project and they will go away.

NetBeans IDE 8.0.1 Compiling & Running A C++ Program

  • To run the Qt Designer so you can modify or create a new form, look in the project's Resource Files folder in the NetBeans Project Window. Right click on the file name HelloForm.ui and choose Open. The Qt Designer will run and show you the project form.

Qt Designer 5.3.1

Now you are up and running!

Note that if you have installed the 32-bit tools, you will have to set up the NetBeans build tools for the 32-bit versions separate from the 64-bit versions. Also note that if you want to build on the command line, you must run C:\Developer\msys64\msys64_shell.bat to build 64-bit programs or C:\Developer\msys64\msys32_shell.bat to build 32-bit programs on the command line.

Additional Information


3 comments:

  1. Hi Mark,

    It's good to see MSYS2 getting some exposure, however I wonder why you don't recommend people to just install our 64bit Qt5 and toolchain packages from our MinGW-w64 packages repository:

    pacman -S mingw-w64-x86_64-qt5 mingw-w64-x86_64-toolchain

    Btw, a word of caution, both Qt-x64 and MSYS2/MinGW-w64's 64bit Qt5 suffer from this (IMHO) very serious bug:

    https://bugreports.qt-project.org/browse/QTBUG-40814

    ReplyDelete
    Replies
    1. I just discovered MSYS2 existed and didn't know much about it other than how to use pacman to install make.exe. I don't work with Qt or C++, I just like installing the tools and getting them going in NetBeans. Thanks for the tips!

      Delete
    2. I updated the article to indicate that Qt-x64 and MinGW-w64 can be installed with the MSYS2 package manager.

      Delete