How to build a QT-project in visual studio from command line - c++

I am trying to automate the build process for one of my QT-project. I used the following command in my batch file for non-QT projects
"C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe" "myproject.sln" /build "Debug|x64" /projectconfig Debug
but it is not working for my QT project. Am I missing something?

Here is an example on how to do that (command by command):
"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat" x86
cd <my_project_directory>
qmake
nmake
The first command sets up the environment for using Visual Studio tools.
Second command changes the current directory to one where your Qt project file is (you have to have one).
Third command runs Qt's qmake.exe utility to generate make files.
And finally nmake will build your project.
However, if you don't use Qt project files and have only VisualStudio solution, you can use MSBuild utility, like:
"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat" x86
MSBuild your_solution.sln /p:Configuration=Debug
You can also set additional environment variables, such as QTDIR if it does not find your Qt installation.

If somebody finds this question looking for an answer on how to automate the build process of a QT project, and wants to do it using a BATCH file as the original question states, here is the BATCH script that I used to automate my building process:
#echo off
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x86
cd %path_to_your_repo%
nmake /f Makefile.Release clean && qmake.exe %path_to_your_.pro% -spec win32-msvc "CONFIG+=qtquickcompiler"
nmake qmake_all
nmake -f Makefile.Release
It is important to call the vcvarsall.bat the first thing, as this will set the environment for all visual studio tools. Also make sure to launch it with call, if you just start the batch file as in #vahancho's answer it will stop your script after executing vcvarsall.bat.
The clean step is not necessary but it is a good practice to use it before building.
It is important to select the -spec and CONFIG (if any) during the qmake step, as this will allow you to select the compiler and required configuration if you are using some extra QT configuration.

Related

building qt project using visual studio

I have build a Qt project (qwt) using the vs2013 compiler in Qt creator. When I open the .pro file and look at the build steps I see:
qmake: qmake.exe qwt.pro -r -spec win32-msvc2013
Make: nmake.exe in C:\qwt-6.1.2
When I build the project, all goes fine and the project is build without errors. So far so good.
However when I try to build this project using the command line I get errors during the nmake step. I have used the same commands as above (and I'm sure that I'm using the same versions of qmake/nmake).
...\QtCore\qglobal.h(38) : fatal error C1083: Cannot open include
file: 'stddef.h': No such file or directory
My question is, what does Qt do more so that the project does build in Qt creator and not from the command prompt? And how can I copy this behaviour in the command prompt? I'm doing this so I can make a batch file that builds the project (on a machine that does not have Qt creator installed).
In order to use the Visual Studio compilation environment from the command-line you need to call the vcvars batch script to set up the paths correctly.
Either call the vcvars64.bat in the C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64 folder (that is for VS2013, you can replace the "12.0" with whatever visual studio version you have).
Or the vcvars32.bat in the C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin folder.
Or use the vcvarsall.bat x86 or vcvarsall.bat x64 calls for the script in C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC
Alternatively Visual Studio creates shortcuts in the start-menu called along the lines of VS2013 x64 Native Tools Command Prompt which call those scripts.
It's likely to be that Qt Creator has added additional variables into the build environment, which are not presented when you try to compile manually through cmd.
If you look within Qt Creator under the Projects section there's Build information. Expand the Build Environment information and check to see if there's anything in there different to your machines global environment settings.

Qt Windows x64 build succeeds in IDE but not on the command line

I have a Qt project I'm trying to automate. On Windows I build for both 32-bit and 64-bit platforms. I am using Visual Studio 2013. I have the following Qt Kits installed: Desktop Qt 5.5.1 MSVC2013 32bit, and Desktop Qt 5.5.1 MSVC2013 64bit. All four versions (32-bit debug and release, and 64-bit debug and release) can be built and run without any trouble from the IDE. Unfortunately, building from the command line fails with this error:
fatal error LNK1112: module machine type 'X86' conflicts with target machine type 'x64'
My command-line system, intended for eventual use in a batch file, and run in my project directory, is:
pushd Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin
call vcvars32.bat
popd
md build64
cd build64
G:\Qt\5.5\msvc2013_64\bin\qmake.exe -spec win32-msvc2013 -o Makefile ..\Maps.pro
G:\Qt\Tools\QtCreator\bin\jom.exe -f Makefile.Release
I use this command in a command window (after setting up the path using vcvars32 so that the Visual Studio tools like nmake, rc and link can be found) and get the error. Note that the command-line method works perfectly for my 32-bit build.
The qmake command line I use is copied from the one recorded in a comment in the Makefile created by the IDE, which claims it is the command used to create that makefile. I know that the "-spec win32-msvc2013" option looks strange, but it works when the IDE uses it.
The jom command line I use is the one used by the IDE, as logged by the Compile Output window.
The only thing I can think of is that Qt sets up some 'magic' environmental variables which I don't know about.
[Yes, I have googled very extensively and looked at other questions and answers here, and can't find anything that fits this case.]
This code
pushd Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin
vcvars32.bat
popd
sets up environment for building for 32-bit architecture. Run vcvars32.bat from different directory (I think it should have amd64 in its name, don't have Windows computer at the moment so can't say the exact directory name, but you'll figure out).
Or do
pushd "<DRIVE>:\Program Files (x86)\Microsoft Visual Studio <Version>\Common7\Tools"
vsvars32 x64
popd
Update for VS2017:
For 64-bit architecture use this command:
"<DRIVE>:\Program Files (x86)\Microsoft Visual Studio\2017\<Version>\VC\Auxiliary\Build\vcvars64.bat"
and for 32 bits:
"<DRIVE>:\Program Files (x86)\Microsoft Visual Studio\2017\<Version>\VC\Auxiliary\Build\vcvars32.bat"

Can't build gdal in x64

I am trying to build GDAL (1.9.2) in x64.
In the instructions, I see:
# Uncomment the following if you are building for 64-bit windows
# (x64). You'll need to have PATH, INCLUDE and LIB set up for 64-bit
# compiles.
!IF "$(PLATFORM)" == "x64"
WIN64=YES
!ENDIF
And then, lower,
# Under win64, symbols for function names lack the underscore prefix
# present on win32. Also the STDCALL calling convention is not used.
!IFDEF WIN64
!UNDEF STDCALL
!ELSE
SYM_PREFIX=_
!ENDIF
Can't find a PATH, INCLUDE and LIB specific for x64, or anything else that I should do...
I can build in Win32.
In x64, I get linker errors:
LINK : error LNK2001: unresolved external symbol _OGRFeatureStylePuller
LINK : error LNK2001: unresolved external symbol _OSRValidate
...
gdal19.dll : fatal error LNK1120: 74 unresolved externals
NMAKE : fatal error U1077: '"c:\Program Files (x86)\Microsoft Visual Studio
10.0\VC\BIN\link.EXE"' : return code '0x460'
I put (to build in Win32)
!IFNDEF PLATFORM
PLATFORM=WIN32
!ENDIF
Modified to
!IFNDEF PLATFORM
PLATFORM=x64
!ENDIF
(to build in x64) - And it worked. But only if I build from inside Visual Studio.
I would like to be able to use a bat file (and build alll platform/configurations)
The above - while it builds in VS, it will not build from command line
(with commands:
start /b /wait nmake -f makefile.vc clean
start /b /wait nmake.exe /f makefile.vc PLATFORM=x64
start /b /wait nmake.exe /f makefile.vc devinstall PLATFORM=x64
The exact same thing builds in Win32...
I can't figure out what is wrong...
I managed to build x64 version of GDAL under Visual Studio 2012 using steps from http://dominoc925.blogspot.ru/2013/03/build-64-bit-gdal-for-windows.html:
Download gdal-1.9.2.tar.gz (or other version of sources) from http://download.osgeo.org/gdal/
Unpack to some directory, e.g. C:\tmp\gdal-1.9.2\
If you tried to build GDAL previously (e.g. x86), make sure that the build directory (C:\warmerda\bld\) and the source directory are clean from previous build attempt. If unsure, try to unpack sources in a new directory.
Start VS2012 x64 Native Tools Command Prompt:
Start -> All Programs -> Microsoft Visual Studio 2012 -> Visual Studio Tools -> Open VS2012 x64 Native Tools Command Prompt
Or run
%comspec% /k "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcvarsall.bat" amd64).
Change directory to the directory with unpacked GDAL sources:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC>cd /D C:\tmp\gdal-1.9.2
D:\trn4\gdal-1.9.2>
Build GDAL with development files:
nmake /f makefile.vc MSVC_VER=1700 WIN64=YES
nmake /f makefile.vc MSVC_VER=1700 WIN64=YES install
nmake /f makefile.vc MSVC_VER=1700 WIN64=YES devinstall
You can get your MSVC_VER number from here. GDAL will be built and installed to C:\warmerda\bld\.
To build with nmake command line I had to run the command from a command shell that has the appropriate variables set to build for 64 bits.
This is what I did to set up the 64 bits building environment:
call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" amd64
You should first somehow make clear to readers of this thread that over the 6 edits you've done yet, the focus of your question changed more than once, as one can see from the revision history
Anyway:
Now it's just a batch vs ide build job issue; try exporting the makefile from VS
EXPORTING! NOT just opening the package orginal makefile.vc in an editor window!
, save it under a name different from the package's "makefile.vc" and use that in your batch.
"I can build in Win32."
Have you had built for Win32 BEFORE your Win64 built attempt?
If so, pls attend
http://trac.osgeo.org/gdal/ticket/4636, bottom of the reply list:
Just to note for anyone else having the issue.. I'll try to look more
into this when I can.. but in the meantime, a clean build is not
really doing a "clean" so make sure you manually delete your files if
you do a 32 bit build before your 64bit build.
You could also try to simply extract the source again at a different directory, in order to have a clean tree and try to build there for Win64.

How to configure Include/Lib directories of VC++ devenv.com command line?

I'm building a VC++ 9.0 project from command line (using devenv.com):
devenv.com myproject.sln /Build "Release|Win32"
I need to add additional include path to it. How can I do this?
I gave up using devenv.com for the same reason. I use msbuild instead.
Add your include and lib to your project
launch vcvarsall.bat
call msbuild.exe
I have Visual Studio 2010. I created a small batch file that is in my path (because I have many dev environments on my computer).
#c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat %*
And I actually wrap msbuild.exe with this simple batch file :
#for %%i in (*.sln) do #msbuild %%i /v:m /nologo %*
With the trailing %*, you can add other parameters when needed.

How to build libevent version 2.0.10 with Visual Studio 2008

Does anyone have clear instructions on building libevent-2.0.10 with Visual Studio 2008?
Use the Visual Studio Command Prompt, then
cd \where\you\put\it
nmake -f makefile.nmake
Or if you prefer the IDE, then File + New + Project, Visual C++, General node, pick the Makefile Project template. Name = libevent-2.0.10-stable, Location = parent directory (\where\you\put). OK. Next. Build command = nmake -f makefile.nmake, rest blank.
Don't forget to run vcvars32.bat before nmake to set up the environment variables for VC.
If you are using the GUI to build it, do not check the option "Create directory for solution" when creating the project.