Qt5 + VS2015 的命令行编译和安装

目录 Content
[hide]

前面很多时候,LT 是在 Qt Creator 中创建项目和编译项目。但是, Qt 也提供了命令行方式,尤其是网上一些Qt项目介绍都是用命令行编译的方式介绍,所以,本文探索了一下命令行的用法。

一、设置

默认情况下,在Qt安装后,会有一个命令行快捷方式,比如我的版本叫做 “Qt 5.12.3 (MSVC 2015 64-bit)”,指向 “ C:\Qt\Qt5.12.3\5.12.3\msvc2015_64\bin\qtenv2.bat” 这个文件。

打开后,内容是这样的:

@echo off
echo Setting up environment for Qt usage...
set PATH=C:\Qt\Qt5.12.3\5.12.3\msvc2015_64\bin;%PATH%
cd /D C:\Qt\Qt5.12.3\5.12.3\msvc2015_64
echo Remember to call vcvarsall.bat to complete environment setup!

需要添加 vcvarsall.bat 来加入VC的环境变量。添加相应变量:

echo call "vcvarsall.bat amd64" to complete environment setup!
"c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64

二、编译

以Creator创建的一个项目 myApp 为例,已经有了一个 myApp.pro 文件。

#-------------------------------------------------
#
# Project created by QtCreator 2019-05-27T15:17:57
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = myApp
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

打开命令行快捷方式,依此键入命令

cd myApp
mkdir build
cd build
qmake ..
nmake

至此,就可以编译成功。可以看到过程类似如下:

D:\myApp\build>qmake ..
Info: creating stash file D:\myApp\build\.qmake.stash

D:\myApp\build>nmake

Microsoft (R) Program Maintenance Utility Version 14.00.24210.0
Copyright (C) Microsoft Corporation.  All rights reserved.

        "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\amd64\nmake.exe" -f Makefile.Release

Microsoft (R) Program Maintenance Utility Version 14.00.24210.0
Copyright (C) Microsoft Corporation.  All rights reserved.

        C:\Qt\Qt5.12.3\5.12.3\msvc2015_64\bin\uic.exe ..\mainwindow.ui -o ui_mainwindow.h
        cl -BxC:\Qt\Qt5.12.3\5.12.3\msvc2015_64\bin\qmake.exe -nologo -Zc:wchar_t -FS -Zc:rvalueCast -Zc:inline -Zc:strictStrings -Zc:throwingNew -O2 -MD -W3 -w34100 -w34189 -w44996 -w44456 -w44457 -w44458 -wd4577 -wd4467 -E C:\Qt\Qt5.12.3\5.12.3\msvc2015_64\mkspecs\features\data\dummy.cpp 2>NUL >release\moc_predefs.h
        C:\Qt\Qt5.12.3\5.12.3\msvc2015_64\bin\moc.exe -DUNICODE -D_UNICODE -DWIN32 -D_ENABLE_EXTENDED_ALIGNED_STORAGE -DWIN64 -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DNDEBUG --compiler-flavor=msvc --include D:/myApp/build/release/moc_predefs.h -IC:/Qt/Qt5.12.3/5.12.3/msvc2015_64/mkspecs/win32-msvc -ID:/myApp -IC:/Qt/Qt5.12.3/5.12.3/msvc2015_64/include -IC:/Qt/Qt5.12.3/5.12.3/msvc2015_64/include/QtWidgets -IC:/Qt/Qt5.12.3/5.12.3/msvc2015_64/include/QtGui -IC:/Qt/Qt5.12.3/5.12.3/msvc2015_64/include/QtANGLE -IC:/Qt/Qt5.12.3/5.12.3/msvc2015_64/include/QtCore -I. -I"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE" -I"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\INCLUDE" -I"C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt" -I"C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um" -I"C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared" -I"C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\um" -I"C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\winrt" ..\mainwindow.h -o release\moc_mainwindow.cpp
        cl -c -nologo -Zc:wchar_t -FS -Zc:rvalueCast -Zc:inline -Zc:strictStrings -Zc:throwingNew -O2 -MD -W3 -w34100 -w34189 -w44996 -w44456 -w44457 -w44458 -wd4577 -wd4467 -EHsc -DUNICODE -D_UNICODE -DWIN32 -D_ENABLE_EXTENDED_ALIGNED_STORAGE -DWIN64 -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DNDEBUG -I..\..\myApp -I. -IC:\Qt\Qt5.12.3\5.12.3\msvc2015_64\include -IC:\Qt\Qt5.12.3\5.12.3\msvc2015_64\include\QtWidgets -IC:\Qt\Qt5.12.3\5.12.3\msvc2015_64\include\QtGui -IC:\Qt\Qt5.12.3\5.12.3\msvc2015_64\include\QtANGLE -IC:\Qt\Qt5.12.3\5.12.3\msvc2015_64\include\QtCore -Irelease -I. -IC:\VulkanSDK\1.0.51.0\include -IC:\Qt\Qt5.12.3\5.12.3\msvc2015_64\mkspecs\win32-msvc -Forelease\ @C:\Users\LT\AppData\Local\Temp\nm1FE3.tmp
main.cpp
mainwindow.cpp
Generating Code...
        cl -c -nologo -Zc:wchar_t -FS -Zc:rvalueCast -Zc:inline -Zc:strictStrings -Zc:throwingNew -O2 -MD -W3 -w34100 -w34189 -w44996 -w44456 -w44457 -w44458 -wd4577 -wd4467 -EHsc -DUNICODE -D_UNICODE -DWIN32 -D_ENABLE_EXTENDED_ALIGNED_STORAGE -DWIN64 -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DNDEBUG -I..\..\myApp -I. -IC:\Qt\Qt5.12.3\5.12.3\msvc2015_64\include -IC:\Qt\Qt5.12.3\5.12.3\msvc2015_64\include\QtWidgets -IC:\Qt\Qt5.12.3\5.12.3\msvc2015_64\include\QtGui -IC:\Qt\Qt5.12.3\5.12.3\msvc2015_64\include\QtANGLE -IC:\Qt\Qt5.12.3\5.12.3\msvc2015_64\include\QtCore -Irelease -I. -IC:\VulkanSDK\1.0.51.0\include -IC:\Qt\Qt5.12.3\5.12.3\msvc2015_64\mkspecs\win32-msvc -Forelease\ @C:\Users\LT\AppData\Local\Temp\nm2582.tmp
moc_mainwindow.cpp
        link /NOLOGO /DYNAMICBASE /NXCOMPAT /INCREMENTAL:NO /SUBSYSTEM:WINDOWS "/MANIFESTDEPENDENCY:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' publicKeyToken='6595b64144ccf1df' language='*' processorArchitecture='*'" /MANIFEST:embed /OUT:release\myApp.exe @C:\Users\LT\AppData\Local\Temp\nm2832.tmp

三、安装

如果编译库或者插件,可以在最后再运行

nmake install

四、实例

以VS2017 64为例 (2109-11-13 补充)

设置 qtenv2.bat

@echo off
echo Setting up environment for Qt usage...
set PATH=C:\Qt\Qt5.12.4\5.12.4\msvc2017_64\bin;%PATH%
cd /D C:\Qt\Qt5.12.4\5.12.4\msvc2017_64
echo Remember to call vcvarsall.bat to complete environment setup!
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\vcvarsall.bat" amd64

以编译 和 安装 led-designer-plugin 为例

Setting up environment for Qt usage...
Remember to call vcvarsall.bat to complete environment setup!
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.9.15
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

C:\Qt\Qt5.12.4\5.12.4\msvc2017_64>cd C:\Users\LT\Desktop\led-designer-plugin\led-designer-plugin\SRC

C:\Users\LT\Desktop\led-designer-plugin\led-designer-plugin\SRC>cd build

C:\Users\LT\Desktop\led-designer-plugin\led-designer-plugin\SRC\build>qmake ..
Info: creating stash file C:\Users\LT\Desktop\led-designer-plugin\led-designer-plugin\SRC\build\.qmake.stash

C:\Users\LT\Desktop\led-designer-plugin\led-designer-plugin\SRC\build>nmake

Microsoft (R) 程序维护实用工具 14.16.27032.1 版
版权所有 (C) Microsoft Corporation。  保留所有权利。

        "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\bin\HostX64\x64\nmake.exe" -f Makefile.Release

Microsoft (R) 程序维护实用工具 14.16.27032.1 版
版权所有 (C) Microsoft Corporation。  保留所有权利。

        C:\Qt\Qt5.12.4\5.12.4\msvc2017_64\bin\rcc.exe -name icon ..\icon.qrc -o release\qrc_icon.cpp
        cl -BxC:\Qt\Qt5.12.4\5.12.4\msvc2017_64\bin\qmake.exe -nologo -Zc:wchar_t -FS -Zc:rvalueCast -Zc:inline -Zc:strictStrings -Zc:throwingNew -Zc:referenceBinding -Zc:__cplusplus -O2 -MD -W3 -w34100 -w34189 -w44996 -w44456 -w44457 -w44458 -wd4577 -wd4467 -E C:\Qt\Qt5.12.4\5.12.4\msvc2017_64\mkspecs\features\data\dummy.cpp 2>NUL >release\moc_predefs.h
        C:\Qt\Qt5.12.4\5.12.4\msvc2017_64\bin\moc.exe -DUNICODE -D_UNICODE -DWIN32 -D_ENABLE_EXTENDED_ALIGNED_STORAGE -DWIN64 -DQT_NO_DEBUG -DQT_PLUGIN -DQT_DESIGNER_LIB -DQT_UIPLUGIN_LIB -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_XML_LIB -DQT_CORE_LIB -DQDESIGNER_EXPORT_WIDGETS -DNDEBUG -D_WINDLL --compiler-flavor=msvc --include C:/Users/LT/Desktop/led-designer-plugin/led-designer-plugin/SRC/build/release/moc_predefs.h -IC:/Qt/Qt5.12.4/5.12.4/msvc2017_64/mkspecs/win32-msvc -IC:/Users/LT/Desktop/led-designer-plugin/led-designer-plugin/SRC -IC:/Users/LT/Desktop/led-designer-plugin/led-designer-plugin/SRC -IC:/Qt/Qt5.12.4/5.12.4/msvc2017_64/include -IC:/Qt/Qt5.12.4/5.12.4/msvc2017_64/include/QtDesigner -IC:/Qt/Qt5.12.4/5.12.4/msvc2017_64/include/QtUiPlugin -IC:/Qt/Qt5.12.4/5.12.4/msvc2017_64/include/QtWidgets -IC:/Qt/Qt5.12.4/5.12.4/msvc2017_64/include/QtGui -IC:/Qt/Qt5.12.4/5.12.4/msvc2017_64/include/QtANGLE -IC:/Qt/Qt5.12.4/5.12.4/msvc2017_64/include/QtXml -IC:/Qt/Qt5.12.4/5.12.4/msvc2017_64/include/QtCore -I. -I"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\ATLMFC\include" -I"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include" -I"C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um" -I"C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt" -I"C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared" -I"C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\um" -I"C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\winrt" -I"C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\cppwinrt" ..\LED.h -o release\moc_LED.cpp
        C:\Qt\Qt5.12.4\5.12.4\msvc2017_64\bin\moc.exe -DUNICODE -D_UNICODE -DWIN32 -D_ENABLE_EXTENDED_ALIGNED_STORAGE -DWIN64 -DQT_NO_DEBUG -DQT_PLUGIN -DQT_DESIGNER_LIB -DQT_UIPLUGIN_LIB -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_XML_LIB -DQT_CORE_LIB -DQDESIGNER_EXPORT_WIDGETS -DNDEBUG -D_WINDLL --compiler-flavor=msvc --include C:/Users/LT/Desktop/led-designer-plugin/led-designer-plugin/SRC/build/release/moc_predefs.h -IC:/Qt/Qt5.12.4/5.12.4/msvc2017_64/mkspecs/win32-msvc -IC:/Users/LT/Desktop/led-designer-plugin/led-designer-plugin/SRC -IC:/Users/LT/Desktop/led-designer-plugin/led-designer-plugin/SRC -IC:/Qt/Qt5.12.4/5.12.4/msvc2017_64/include -IC:/Qt/Qt5.12.4/5.12.4/msvc2017_64/include/QtDesigner -IC:/Qt/Qt5.12.4/5.12.4/msvc2017_64/include/QtUiPlugin -IC:/Qt/Qt5.12.4/5.12.4/msvc2017_64/include/QtWidgets -IC:/Qt/Qt5.12.4/5.12.4/msvc2017_64/include/QtGui -IC:/Qt/Qt5.12.4/5.12.4/msvc2017_64/include/QtANGLE -IC:/Qt/Qt5.12.4/5.12.4/msvc2017_64/include/QtXml -IC:/Qt/Qt5.12.4/5.12.4/msvc2017_64/include/QtCore -I. -I"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\ATLMFC\include" -I"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include" -I"C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um" -I"C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt" -I"C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared" -I"C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\um" -I"C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\winrt" -I"C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\cppwinrt" ..\LEDPlugin.h -o release\moc_LEDPlugin.cpp
        cl -c -nologo -Zc:wchar_t -FS -Zc:rvalueCast -Zc:inline -Zc:strictStrings -Zc:throwingNew -Zc:referenceBinding -Zc:__cplusplus -O2 -MD -W3 -w34100 -w34189 -w44996 -w44456 -w44457 -w44458 -wd4577 -wd4467 -EHsc -DUNICODE -D_UNICODE -DWIN32 -D_ENABLE_EXTENDED_ALIGNED_STORAGE -DWIN64 -DQT_NO_DEBUG -DQT_PLUGIN -DQT_DESIGNER_LIB -DQT_UIPLUGIN_LIB -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_XML_LIB -DQT_CORE_LIB -DQDESIGNER_EXPORT_WIDGETS -DNDEBUG -D_WINDLL -I..\..\SRC -I. -I..\..\SRC -IC:\Qt\Qt5.12.4\5.12.4\msvc2017_64\include -IC:\Qt\Qt5.12.4\5.12.4\msvc2017_64\include\QtDesigner -IC:\Qt\Qt5.12.4\5.12.4\msvc2017_64\include\QtUiPlugin -IC:\Qt\Qt5.12.4\5.12.4\msvc2017_64\include\QtWidgets -IC:\Qt\Qt5.12.4\5.12.4\msvc2017_64\include\QtGui -IC:\Qt\Qt5.12.4\5.12.4\msvc2017_64\include\QtANGLE -IC:\Qt\Qt5.12.4\5.12.4\msvc2017_64\include\QtXml -IC:\Qt\Qt5.12.4\5.12.4\msvc2017_64\include\QtCore -Irelease -I\include -IC:\Qt\Qt5.12.4\5.12.4\msvc2017_64\mkspecs\win32-msvc -Forelease\ @C:\Users\LT\AppData\Local\Temp\nmA195.tmp
LED.cpp
LEDPlugin.cpp
Header <QtDesigner/QDesignerCustomWidgetInterface> is deprecated. Please include <QtUiPlugin/QDesignerCustomWidgetInterface> instead.
正在生成代码...
        cl -c -nologo -Zc:wchar_t -FS -Zc:rvalueCast -Zc:inline -Zc:strictStrings -Zc:throwingNew -Zc:referenceBinding -Zc:__cplusplus -O2 -MD -W3 -w34100 -w34189 -w44996 -w44456 -w44457 -w44458 -wd4577 -wd4467 -EHsc -DUNICODE -D_UNICODE -DWIN32 -D_ENABLE_EXTENDED_ALIGNED_STORAGE -DWIN64 -DQT_NO_DEBUG -DQT_PLUGIN -DQT_DESIGNER_LIB -DQT_UIPLUGIN_LIB -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_XML_LIB -DQT_CORE_LIB -DQDESIGNER_EXPORT_WIDGETS -DNDEBUG -D_WINDLL -I..\..\SRC -I. -I..\..\SRC -IC:\Qt\Qt5.12.4\5.12.4\msvc2017_64\include -IC:\Qt\Qt5.12.4\5.12.4\msvc2017_64\include\QtDesigner -IC:\Qt\Qt5.12.4\5.12.4\msvc2017_64\include\QtUiPlugin -IC:\Qt\Qt5.12.4\5.12.4\msvc2017_64\include\QtWidgets -IC:\Qt\Qt5.12.4\5.12.4\msvc2017_64\include\QtGui -IC:\Qt\Qt5.12.4\5.12.4\msvc2017_64\include\QtANGLE -IC:\Qt\Qt5.12.4\5.12.4\msvc2017_64\include\QtXml -IC:\Qt\Qt5.12.4\5.12.4\msvc2017_64\include\QtCore -Irelease -I\include -IC:\Qt\Qt5.12.4\5.12.4\msvc2017_64\mkspecs\win32-msvc -Forelease\ @C:\Users\LT\AppData\Local\Temp\nmB50E.tmp
qrc_icon.cpp
moc_LED.cpp
moc_LEDPlugin.cpp
Header <QtDesigner/QDesignerCustomWidgetInterface> is deprecated. Please include <QtUiPlugin/QDesignerCustomWidgetInterface> instead.
正在生成代码...
        link /NOLOGO /DYNAMICBASE /NXCOMPAT /INCREMENTAL:NO /DLL /SUBSYSTEM:WINDOWS /OUT:release\led-designer-plugin.dll @C:\Users\LT\AppData\Local\Temp\nmC200.tmp
  正在创建库 release\led-designer-plugin.lib 和对象 release\led-designer-plugin.exp

C:\Users\LT\Desktop\led-designer-plugin\led-designer-plugin\SRC\build>nmake install

Microsoft (R) 程序维护实用工具 14.16.27032.1 版
版权所有 (C) Microsoft Corporation。  保留所有权利。

        "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\bin\HostX64\x64\nmake.exe" -f Makefile.Release install

Microsoft (R) 程序维护实用工具 14.16.27032.1 版
版权所有 (C) Microsoft Corporation。  保留所有权利。

        copy /y release\led-designer-plugin.dll C:\Qt\Qt5.12.4\5.12.4\msvc2017_64\plugins\designer\led-designer-plugin.dll
已复制         1 个文件。

C:\Users\LT\Desktop\led-designer-plugin\led-designer-plugin\SRC\build>


延伸阅读

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.