How to build Qt for Visual Studio 2010 - c++

I struggled finding a how-to which provides a stable solution for using Qt with Visual Studio 2010, so after collecting all the bits of information and some trial and error, I would like to write my solution into a guide.
The problem, or why is it not possible to use prebuilt binaries?
It seems that using binaries built for Visual Studio 2008 might work in some special cases, but I found them not to work. In my case they compiled OK, but they produce runtime errors, like this:
or when started from Visual Studio 2010:
Update: I found a blog post analysing why does it work for some people, while it does not for others. In one word, it depends on whether you have Visual Studio 2008 installed on the same machine, or not.
http://blog.paulnettleship.com/2010/11/11/troubleshooting-visual-studio-2010-and-qt-4-7-integration/
The most important thing (that I stupidly didn’t realize) was the fact that you CANNOT use the Visual Studio 2008 compiled libraries and dll’s (available on the Qt webpage) if you don’t have Visual Studio 2008 installed. The reason is because the Qt SDK you download is a debug build which is dependant on the VC9.0 DebugCRT, meaning it needs the Visual C++ 2008 Debug Runtime installed, which is NOT available as a redistributable installer. The only way to install the DebugCRT is to install the entirety of Visual Studio 2008.

First of all, it’s very important to understand that for using Qt with Visual Studio 2010, it's not possible to use the pre-built binaries which were made for Visual Studio 2008, but you have to compile it from source.
Downloading Qt
On https://www.qt.io/download/
Update 2017: the latest Qt 4.x branch (Qt 4.8.6) has 2 pre-built packages, which are now in the archive section:
http://download.qt.io/archive/qt/4.8/4.8.6/qt-opensource-windows-x86-vs2010-4.8.6.exe
http://download.qt.io/archive/qt/4.8/4.8.6/qt-opensource-windows-x86-vs2008-4.8.6.exe
You should not download Qt by clicking "Qt libraries 4.8.6 for Windows (Visual Studio 2008, 218 MB)", but by clicking on the "zip" link above it.
On that link, you get a big zip file like "qt-everywhere-opensource-src-4.8.6.zip". Unzip this into a folder and make its path something nice and small, for example "E:\Qt"
Visual Studio Command Prompt
Now that we have the sources, we need to build the binaries. To do it, open the Microsoft Visual Studio 2010\Visual Studio Tools\Visual Studio Command Prompt (2010) link from your start menu, or even pin it to the taskbar (a good idea). This is a special command prompt which has all the variables set for building with Visual Studio 2010 tools.
Once within the command prompt, navigate to your extracted Qt folder using old-school DOS way, which means you have to change drive letter by E:, enter directories by cd Qt and list dir contents by dir. You can use the tab key for helping you with the directory names. When you have arrived at the correct directory, a dir command should return something like this.
Building Qt
Now it’s time for configure and build. For configuring a minimalist Qt, I'm using the following flags with configure.exe. Just copy and paste it into the command line. Look in the Qt reference manual for what flag to use or not to use.
configure.exe -release -no-webkit -no-phonon -no-phonon-backend -no-script -no-scripttools -no-qt3support -no-multimedia -no-ltcg
Once configure.exe has finished (it was 10 minutes for me), you'll need to start the build process. It will take about 20-30 minutes with the above flags. To start it, just type:
nmake
Setting environment variables
Basically, we are done. All you need to do is to set your environment variables (QTDIR and PATH), which tell programs where to find Qt. If you are on Windows 7, you can use the following command to set QTDIR to your installation dir.
setx QTDIR e:\Qt
For setting the PATH, I strongly recommend using Path Editor. Within Path Editor
add the directory of Qt\bin to your PATH
(it doesn't matter if it's in system path or user path)
If you prefer to use Control Panel\System\Environment Variables, then you can set these there, too.
Qt Visual Studio Add-in
Here you go, after a logoff-logon or a restart, all the Qt demo applications should start correctly (I recommend have a look at bin\qtdemo.exe). Now you can download and install the Visual Studio Add-in (qt-vs-addin-1.1.9.exe) from the Qt download page, it will work perfectly.
Appendix A: Official Instructions:
There is a page at the official wiki at the Qt website called Qt 4.8 Installing Qt for Windows, but I found it lacking important information.
References
Qt DevNet forums
Recommended flags for a minimalistic Qt build
Building Qt 4.5 with Visual C++ 2010
How to compile Qt as static
Qt 4.8: Configure options for Qt
Edit the PATH environment variable in
Windows without pain - op111.net

Qt V4.8.0 contains prebuilt binaries for Visual Studio 2010 so you don't need to do this anymore:
http://qt.nokia.com/downloads/windows-cpp-vs2010

Refer below link and it might be useful
1)https://www.slicer.org/slicerWiki/index.php/Documentation/Nightly/Developers/Build_Instructions/Prerequisites/Qt#Windows_3
2)http://eecs.vanderbilt.edu/research/hmtl/wp/index.php/qt-vs/

Related

Visual Studio: copy installation folder and setup it up to be auto-detected by Qt Creator

I wiped out an old Windows 10 and replaced it with a new Windows 10 by ISO image.
Before wiping out, I copied the folder of Microsoft Visual Studio 2017 Community Edition on old Windows to an external hard disk:
xcopy /E "C:\Program Files (x86)\Microsoft Visual Studio" D:\
Now after reinstalling Windows 10, I copied the VS 2017 folder from external the hard disk to the C:\Program Files (x86)\ folder of new operating system.
I did so to avoid having to download VS 2017 again on new OS.
The problem is other software, like Qt Creator, cannot auto-detect the VS C/C++ compilers. I guess that's because the VS 2017 isn't added to path.
The questions are:
What path should I add to system path in order to VS 2017 to be auto-detected by other software like Qt Creator?
Is there any script which I can run to automatically integrate the VS 2017 to the rest of the operating system.
To answer the questions in your issue:
Copying the content of C:\Program Files (x86)\Microsoft Visual Studio folder is not a recommended way to install VS.
(The complete installation will not only set the Environment variables but also set the related registry keys and values. A simple copy may break this process)
So we always suggest that users install the VS by vs-installer or offline installation package. (Same like what Zlatomir suggests.)
To your actual requirements:
It seems you're just trying to use VC++ compiler in QT Creator instead of developing QT projects in Visual Studio. So build tools for VS package is enough for you.
See this related issue, if we download Build Tools for VS package with corresponding C++ workload, then we can get the compiler the QT needs. There's no need to install the VS IDE for this situation.
And if we need C++ compiler from VS2017, download the Build Tools for VS2017, if we need C++ compiler from VS2015, download the Build Tools for VS2015 Update3. Link of old version about VS Tools see here.
Update:
From the the link older downloads we can see:
Choose the download button according to which version we need. We can find both VS2017 build tools package and VS2015's there.
Usually programs on Windows don't work after you just copy the installation folder.
So right now there is little you can do to fix it, even if you partially fix, you have no way to know if something else will be broken in the future.
So the recommended solution is to download the installer again and create an offline installer for the next time this happens, you can do that by running the online installer with the following parameters: vs_community.exe --layout c:\vslayout --lang en-US (replace vs_community.exe with the actual name of the online installer file, or rename it), for more options, that might require a smaller download (example if you only need native and don't need .net) check the documentation here for the full set of options you have.

QT and native OpenGL support in MS Windows

I'm trying to import the cube example in MS visual studio 2013 via the QT add-in.
Unfortunately, when trying to open the .pro file I get the following error:
Project ERROR: This example requires Qt to be configured with -opengl desktop
Doing a little research in the web I realized that by default QT OpenGL support is build on ANGLE-library which as I realized is an OpenGL to DirectX9.0 kind of translator.
Thus, I have two options (link):
Install ANGLE.
Recompile QTv5.3 with option -opengl desktop.
The problem is that I installed QTv5.3 via QT online installer for windows.
Q:
Considering the above, how am I supposed to recompile QTv5.3 with native OpenGL support (i.e., with -opengl desktop option) when I've installed QTv5.3 via the windows online installer?
Although you can compile it yourself with any flags you wish, there is a simpler option.
Qt online installer provides several distributions: different versions of Qt, for different targets, with or without OpenGL support.
Easiest way to install Qt 5.3.2 MSVC 2013 OpenGL:
run <QT_FOLDER>\MaintenanceTool.exe (or online installer if you have never installed Qt yet)
choose Package manager, press Next
Under Qt->5.3 check msvc2013 32-bit OpenGL (along with any other version you need)
Press Next...
It will be installed in <QT_FOLDER>\msvc2013_opengl
Then you probably will need to pointVisual Studio AddIn to correct Qt folder (it can carry multiple versions).
Easiest way to make Qt and MSVC friends
You can easily generate Visual Studio .vcxproj file from Qt' project .pro file by running qmake. No any addons required.
To do this:
open VS2013 x86 Native Tools Command Prompt (or set up path with "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x86)
add desired Qt version to path:
set PATH=%PATH%;<QT_FOLDER>\msvc2013_opengl\bin
go to Qt project folder and generate MSVC project:
cd <pro_folder>
qmake -tp vc
You will find in the same folder a .vcxproj file with all Qt's bells and whistles (moc'ing, uic'ing, rcc'ing etc.) set up like build events
Repeat this operation whenever you change .pro file. Reload .vcxproj each time (no need to close Visual Studio; it will ask you to reload when it detects changes). Consider .vcxproj as temporary, generated file and don't change it manually. Tweak all the stuff in .pro.
Hint: write .bat file to do all this stuff automatically.
HTH
You'll need to download and build either the 32-bit version or the 64-bit version.
I built from scratch, but for ANGLE, not OpenGL.
This page should be read before you decide on going either direction.

'cl' is not recognized as an internal or external command,

I am trying to compile a hello world program in Qt Using Qt Creator.
I am getting 'cl' is not recognized as an internal or external command.
I am using Windows 7 and both VS 2008 and 2010 installed in it.
When I use Mingw it is compiling fine but if use vs 2008 it is giving this error.
After Setting Env Path = ..;..;C:\Program Files\Microsoft Visual Studio 9.0\VC\bin also it is showing the same error.
That error happens because cl isn't in your path. You need to add it there. The recommended way to do this is to launch a developer command prompt.
Quoting the article Use the Microsoft C++ toolset from the command line:
On the desktop, open the Windows Start menu. In Windows 11, choose the All apps button to open the list of installed apps. In Windows 10,
the list is open to the left. Scroll down the list to find and open
the folder (not the app) for your version of Visual Studio, for
example, Visual Studio 2022.
In the folder, choose the Developer Command Prompt for your version of Visual Studio. This shortcut starts a developer command
prompt window that uses the default build architecture of 32-bit,
x86-native tools to build 32-bit, x86-native code. If you prefer a
non-default build architecture, choose one of the native or cross
tools command prompts to specify the host and target architecture.
For an even faster way to open a developer command prompt, enter
developer command prompt in the desktop search box. Then choose the
result you want.
As the article notes, there are several different shortcuts for setting up different toolsets - you need to pick the suitable one.
If you already have a plain Command Prompt window open, you can run the batch file vcvarsall.bat with the appropriate argument to set up the environment variables. Quoting the same article:
At the command prompt, use the CD command to change to the Visual Studio installation directory. Then, use CD again to change to the
subdirectory that contains the configuration-specific command files.
For Visual Studio 2019 and Visual Studio 2017, use the
VC\Auxiliary\Build subdirectory. For Visual Studio 2015, use the VC
subdirectory.
Enter the command for your preferred developer environment. For example, to build ARM code for UWP on a 64-bit platform, using the
latest Windows SDK and Visual Studio compiler toolset, use this
command line:
vcvarsall.bat amd64_arm uwp
From the article, the possible values for the first argument are the following:
x86 (x86 32-bit native)
x86_amd64 or x86_x64 (x64 on x86 cross)
x86_arm (ARM on x86 cross)
x86_arm64 (ARM64 on x86 cross)
amd64 or x64 (x64 64-bit native)
amd64_x86 or x64_x86 (x86 on x64 cross)
amd64_arm or x64_arm (ARM on x64 cross)
amd64_arm64 or x64_arm64 (ARM64 on x64 cross)
I had the same problem.
Try to make a bat-file to start the Qt Creator. Add something like this to the bat-file:
call "C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\vcvars32.bat"
"C:\QTsdk\qtcreator\bin\qtcreator"
Now I can compile and get:
jom 1.0.8 - empower your cores
11:10:08: The process "C:\QTsdk\qtcreator\bin\jom.exe" exited normally.
Make sure you restart your computer after you install the Build Tools.
This was what was causing the error for me.
I had the same problem and I solved it by switching to MinGW from MSVC2010.
Select the Project Tab from your left pane. Then select the "Target". From there change Qt version to MinGW instead of VC++.
You will have to set environmental variables properly for each compiler. There are commands on your Program menu for each compiler that does that, while opening a command prompt.
Another option is of course to use the IDE for building your application.
I had this problem because I forgot to select "Visual C++" when I was installing Visual Studio.
To add it, see: https://stackoverflow.com/a/31568246/1054322
I had the same issue for a long time and I spent God knows how much on it until I accidentally figured what to do. This solution worked on windows 10. All you need to do is to add C:\WINDOWS\System32 to Path variable under User Variables in Environmental Variables... Note that if you add this to the system variables, it may also work. But, that didn't work for me.
You can use Command prompt for VS 2010 and then select the path that your boost located. Use "bootstrap.bat", you can successfully install it.
For me, this was related to the scenario described by Smi, with the difference being that vcvarsall.bat itself was failing due to an invalid path.
This was cause by line-breaks within the path, which meant vcvarsall.bat couldn't find the common tools directory for some reason.
I sometimes get this problem when changing from Debug to Release or vice-versa. Closing and reopening QtCreator and building again solves the problem for me.
Qt Creator 2.8.1; Qt 5.1.1 (MSVC2010, 32bit)
I faced this error too, checked that there are multiple versions of common tool variables for visual studio in my environment variables (VS120COMNTOOLS, VS110COMNTOOLS and so on) for different visual studio versions.
I removed the ones I did not need from the environmental variables and the issue was resolved.
I was facing the same issue and tried many solutions but nothing work ( I was using vscode 2017) . I just upgraded vscode to 2022 version and the problem is automatically

Visual C++ 2010 Express: How to use with Qt?

I'm confused about getting Qt working with Visual Studio Express. Most of the searches online have turned up instructions for compiling Qt using instructions such as:
Install MSVC2005 or MSVC2008
Install the most recent Platform SDK (# MSDN: "Platform SDK for Windows
Server 2003 R2")
From the Platform SDK, you'll need to install at least the base +
internet options
Then, download and extract a recent snapshot or the preview release to
D:\Qt\4.x.x
Open up a console with the MSVC settings loaded (see MSVC Tools-menu)
and go to your Qt directory
Type "configure -platform win32-msvc2005" without the quotation
marks
That will build you the project files you'll need
After that, it's as simple as running "nmake" and drinking a lot of
coffee
Then, add D:\Qt\4.x.x\bin and
D:\Qt\4.x.x\lib to your path
environment variable, and you'll be
able to use this version of Qt from
everywhere on your system. The latter
could be added to the library section
in the options from MSVC as well.
(from http://www.qtcentre.org/threads/11710-Visual-Studio-2005-Express)
However, when I go to the download page of Qt (http://qt.nokia.com/downloads), there is a download for Visual Studio labeled:
"Qt libraries 4.7.1 for Windows (VS 2008, 228 MB)"
I'm confused, do I actually still need to build it? Or is it already built?
Using the downloadable exe from qt (http://get.qt.nokia.com/qt/source/qt-win-opensource-4.7.1-vs2008.exe) does not seem to work with Visual Studio 2010. I tried building a simple hello world program from the command line which would not run. I'll repeat my comment from a previous answer.
I tried the hello world example from
doc.qt.nokia.com/4.7/gettingstartedqt.html.
I was able to build the project and an
exe is generated. However, the exe
fails to run and generates a system
error dialog box about "side-by-side
configuration is incorrect...". I ran
SxsTrace and it says:
...
INFO: Begin assembly probing.
INFO: Did not find the assembly in WinSxS.
INFO: Attempt to probe manifest at C:\Windows\assembly\GAC_32\Microsoft.VC90.DebugCRT\ 9.0.21022.8__...\Microsoft.VC90.DebugCRT.DLL.
INFO: Attempt to probe manifest at C:\qthello\debug\Microsoft.VC90.DebugCRT.DLL.
INFO: Attempt to probe manifest at C:\qthello\debug\Microsoft.VC90.DebugCRT.MANIFEST.
INFO: Attempt to probe manifest at C:\qthello\debug\Microsoft.VC90.DebugCRT\Microsoft.VC90.DebugCRT.DLL.
INFO: Attempt to probe manifest at C:\qthello\debug\Microsoft.VC90.DebugCRT\Microsoft.VC90.DebugCRT.MANIFEST.
INFO: Did not find manifest for culture Neutral.
INFO: End assembly probing.
ERROR: Cannot resolve reference Microsoft.VC90.DebugCRT,processorArchitecture="x86", publicKeyToken="...",type="win32",version="9.0.21022.8".
ERROR: Activation Context generation failed.
End Activation Context Generation.
To me, it looks like its trying to use
VS2008 (which I don't have). It makes
sense I guess since the download is
for VS2008.
I was in the process of compiling qt 4.7.1 using the instructions in my question (using win32-msvc2010 as my platform target however) as I asked this SO question and the compilation finished. I rebuilt the hello world project and the exe runs. So it seems like the answer is in order to use with Visual Studio Express 2010 you currently do need to compile (at least until Qt releases a VS2010 download option). However, I believe if you are using VS2008 then you will not need to compile.
The Qt binaries provided by Nokia for Windows target either MinGW or Visual Studio 2008, both for compiling 32bit executables. If you need Qt binaries for VS 2005 (32bit), you may find them in their archives.
For everything else, you need to compile Qt yourself. Especially for:
Visual Studio 2008 64 bit
Visual Studio 2008 Express Edition 64 bit: You need the Windows SDK 7.0 which provides the 64 bit compilers and compile Qt with those.
Visual Studio 2010 32 bit and 64 bit
In general, you cannot link libraries compiled with different compilers, i.e. use the Qt binaries built with VC 9.0 (VS 2008) 32 bit and link them to an executable built with VC 10.0 (VS 2010) 32 bit.
Just a small update on this.
Downloading the QTSDK (and using cmake instead of qmake everything looks like working quite well. Except for the fact that the installer of QTSDK didn't update the path for qmake.exe and you've to do by hand.
Tested with QtSDK 4.8 and visual studio express 2010
http://qt.nokia.com/downloads has a link called qt-vs-addin-1.1.7.exe (57 MB)
You'll probably need that one as well . .
I don't think you need to compile the libraries since there is a download for them now . .
So get the 228 MB of libs and 57 MB plugin and you should be set.
DISCLAIMER: I don't use MSVC (yet?) so please let me know if it works.

Instruction for installing an environment with Qt and Qt integration with Visual Studio 2008

I want to use Qt lib but I don't know how to install Visual Studio 2008+Qt+Qtintegration. I have read some forums, that step by step tell what to download, where to download from, and then what to do. But that was for old versions of mentiond products.
I ask to Qt developers tell me the way to install these items on Windows. Any forum or site will be fine.
Thanks.
To get QT working with dev studio you need to install two things.
First, a copy of the QT Visual Studio Libraries.
And the QT Visual Studio Addin.
http://qt.nokia.com/downloads/windows-cpp-vs2008
http://qt.nokia.com/downloads/visual-studio-add-in
Download the QT sdk, and install it. It usually installs into a folder like
c:\qt\4.6.2
Then, install the visual studio addin. The addin should add (At least) two new things to Visual Sutdio. A new "Qt" project in the Create New Project Wizard, and a Qt menu.
Qt supports building your project against multiple builds of Qt, so the first thing you need to do is go to the Qt menu, and point it to the directory you installed the Qt SDK to. It all went well it should auto populate with c:\qt\4.6.2
Then, use the Wizard to create a new Qt project in Visual Studio.
If you are creating your first Qt project in Visual Studio this way, there is no need to fiddle with .pro files or doing any kind of "stuff" outside Visual Studio to get a project going. The Visual Studio addin sets up the workspace to do all the pre-compiles needed by the Qt build environment.
--
Note: The Qt SDK at this URL
http://qt.nokia.com/downloads/sdk-windows-cpp
comes with the MinGW runtine for GCC, and Qt Creator embedded. This is no good for Visual Studio. This build installs to a folder like
c:\Qt\2010.2