简体   繁体   English

如何从命令行在 Visual Studio 中构建 QT 项目

[英]How to build a QT-project in visual studio from command line

I am trying to automate the build process for one of my QT-project.我正在尝试自动化我的一个 QT 项目的构建过程。 I used the following command in my batch file for non-QT projects我在批处理文件中对非 QT 项目使用了以下命令

"C:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\Common7\\IDE\\devenv.exe" "myproject.sln" /build "Debug|x64" /projectconfig Debug "C:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\Common7\\IDE\\devenv.exe" "myproject.sln" /build "Debug|x64" /projectconfig 调试

but it is not working for my QT project.但它不适用于我的 QT 项目。 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. 第一个命令设置使用Visual Studio工具的环境。 Second command changes the current directory to one where your Qt project file is (you have to have one). 第二个命令将当前目录更改为Qt项目文件所在的目录(您必须拥有一个目录)。 Third command runs Qt's qmake.exe utility to generate make files. 第三个命令运行Qt的qmake.exe实用程序以生成make文件。 And finally nmake will build your project. 最后, nmake将构建您的项目。

However, if you don't use Qt project files and have only VisualStudio solution, you can use MSBuild utility, like: 但是,如果您不使用Qt项目文件并且只有VisualStudio解决方案,则可以使用MSBuild实用程序,例如:

"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. 如果找不到Qt安装,还可以设置其他环境变量,例如QTDIR

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:如果有人发现这个问题正在寻找有关如何自动化 QT 项目的构建过程的答案,并且想要使用原始问题所述的 BATCH 文件来执行此操作,那么这里是我用来自动化构建过程的 BATCH 脚本:

@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.首先调用vcvarsall.bat很重要,因为这将为所有 Visual Studio 工具设置环境。 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 .还要确保使用call启动它,如果您只是按照@vahancho 的答案启动批处理文件,它将在执行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.qmake步骤期间选择-specCONFIG (如果有)很重要,因为如果您使用一些额外的 QT 配置,这将允许您选择编译器和所需的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM