【Qt】一文总结QtCreator中MSVC编译套件

开篇

在QtCreator中使用MSVC编译套件构建项目时,在Kits对话框中编译器一栏会识别出许多MSVC编译套件,在实际项目开发中,如何选择这些编译套件呢,本文就这个问题展开,主要内容如下:

(1)描述Visual Studio下的命令行工具的环境变量和几种命令提示符。

(2)描述Visual Studio的命令文件。

(3)描述QtCreator对MSVC编译套价的识别。

(注1:本文的前提是已经完成对Visual Studio集成开发工具的安装,如果没有安装,QtCreator将会找不到MSVC相关的编译套件!)

(注2:本文基于完整安装的Visual Studio集成开发工具。单独安装MSVC编译套件不在本文讨论范围内!哈哈)

一、Visual Studio命令行生成的路径和环境变量

MSVC的命令行工具会使用 PATH、TMP、INCLUDE、LIB 和 LIBPATH环境变量,还会使用一些特定的已安装工具、平台和 SDK 的环境变量。 即使是简单 Visual Studio 安装,也会设置二十多个或更多的环境变量。 这些环境变量非常多,所以如果在Windows环境变量中自行单个添加,可能会把自己折腾惨,哈哈!所以在使用MSVC的命令行工具时,必须使用命令提示符快捷方式自定义命令文件来设置环境变量。 不建议在Windows环境中自行设置环境变量。

在windows的命令行和MSVC命令行下分别输入set命令,可以查看环境变量,如下图所示:

【Qt】一文总结QtCreator中MSVC编译套件_第1张图片

左边是windows的命令行,右边是MSVC命令行,可见在MSVC命令行下新建了许多环境变量(PATH、TMP、INCLUDE、LIB 和 LIBPATH)且添加了很多值。这些环境变量,在我们使用命令行来构建项目工程时,都会使用到,缺一不可!

二、Visual Studio命令提示符

命令提示符本质是对windows命令行的一个脚本。在安装Visual Studio后,命令提示符快捷方式会安装在Windows“开始”菜单中特定版本的 Visual Studio 文件夹中,大致结构如下:

  • x86 本机工具命令提示符 - 将环境设置为使用 32 位 x86 本机工具生成 32 位 x86 本机代码。
  • x64 本机工具命令提示符 - 将环境设置为使用 64 位 x64 本机工具生成 64 位 x64 本机代码。
  • x86_x64 兼容工具命令提示符 - 将环境设置为使用 64 位 x64 本机工具生成 32 位 x86 本机代码。
  • x64_x86 兼容工具命令提示符 - 将环境设置为使用 64 位 x64 本机工具生成 32 位 x86 本机代码。

(注:由于选择的工具负载不同,命令提示符会有所不同,本文选择的是C++桌面应用构建相关的负载,且只选择了几种工具套件组合)

安装后,结果如下图所示:

【Qt】一文总结QtCreator中MSVC编译套件_第2张图片

【备注】多个命令行工具或工具选项可能需要管理员权限。 如果在使用这些命令行时遇到权限问题,建议使用“以管理员身份运行”选项打开开发人员命令提示窗口 。 右键单击以打开命令提示窗口的快捷菜单,然后依次选择“更多”、“以管理员身份运行” 。

三、Visual Studio命令文件位置

对于 2019 Visual Studio,64 位系统上的典型安装位置是位于\Program Files\Microsoft Visual Studio\2022下,其中 可能是Community、Professional、Enterprise、BuildTools 或自定义名称。

开发人员的命令提示符命令文件 VsDevCmd.bat位于 Common7\Tools 子目录中。如果未指定参数,该命令文件会设置使用x86 生成 32 位 x86 代码的开发环境。

命令文件将设置默认参数并调用 VsDevCmd.bat脚本去设置指定的生成体系结构环境。 典型安装可能包括以下命令文件:

【Qt】一文总结QtCreator中MSVC编译套件_第3张图片

注意vcvarsall.bat 文件和其他 Visual Studio 命令文件可能因计算机而异。 不要使用另一台计算机中的文件替换已经丢失或损坏的 vcvarsall.bat 文件。 需要重新运行 Visual Studio 安装程序来替换丢失的文件。vcvarsall.bat 文件在不同的版本中也会有所不同。 如果安装了当前版本 Visual Studio 的计算机上也安装了 Visual Studio 的早期版本,则不要在同一命令提示符窗口中运行不同版本的 vcvarsall.bat 或其他 Visual Studio 命令文件。

vcvarsall.bat是Windows下的批处理命令,如下脚本代码所示(不同版本的 Visual Studio会有所不同):

@if not "%VSCMD_DEBUG%" GEQ "3" echo off

@REM -------------------------------------------------------------------------
:parse_args
set __VCVARSALL_STORE=
set __VCVARSALL_WINSDK=
set __VCVARSALL_PARSE_ERROR=
set __VCVARSALL_TARGET_ARCH=
set __VCVARSALL_HOST_ARCH=
set __VCVARSALL_VER=

@REM Parse command line arguments.  This implementation does not care about
@REM argument order.

if "%VSCMD_DEBUG%" GEQ "1" (
    @echo [DEBUG:%~nx0] init with arguments '%*'
)

set __VCVARSALL_PARSE_ERROR=0
set "__VCVARSALL_ARGS_LIST=%*"
call :parse_loop
set __VCVARSALL_ARGS_LIST=

if "%VSCMD_DEBUG%" GEQ "1" (
    @echo [DEBUG:%~nx0] Command line parse completed with values:
    @echo [DEBUG:%~nx0] __VCVARSALL_TARGET_ARCH='%__VCVARSALL_TARGET_ARCH%'
    @echo [DEBUG:%~nx0] __VCVARSALL_HOST_ARCH='%__VCVARSALL_HOST_ARCH%'
    @echo [DEBUG:%~nx0] __VCVARSALL_WINSDK='%__VCVARSALL_WINSDK%'
    @echo [DEBUG:%~nx0] __VCVARSALL_STORE='%__VCVARSALL_STORE%'
    @echo [DEBUG:%~nx0] __VCVARSALL_HELP='%__VCVARSALL_HELP%'
    @echo [DEBUG:%~nx0] __VCVARSALL_PARSE_ERROR='%__VCVARSALL_PARSE_ERROR%'
)

if "%__VCVARSALL_CLEAN_ENV%" NEQ "" goto :call_vsdevcmd
if "%__VCVARSALL_PARSE_ERROR%" NEQ "0" goto :usage_error
if "%__VCVARSALL_HELP%" NEQ "" goto :usage

@REM -------------------------------------------------------------------------
:check_platform
@REM This script is installed to ...\VC\Auxiliary\Build.
@REM vsdevcmd is installed to    ...\Common7\Tools.
if not exist "%~dp0..\..\..\Common7\Tools\vsdevcmd.bat" goto missing

@REM Assemble the arguments to pass to vsdevcmd.bat
if "%__VCVARSALL_TARGET_ARCH%" == "" goto :usage_error
if "%__VCVARSALL_HOST_ARCH%" == "" goto :usage_error

set "__VCVARSALL_VSDEVCMD_ARGS=-arch=%__VCVARSALL_TARGET_ARCH% -host_arch=%__VCVARSALL_HOST_ARCH%"
if "%__VCVARSALL_WINSDK%" NEQ "" (
    set "__VCVARSALL_VSDEVCMD_ARGS=%__VCVARSALL_VSDEVCMD_ARGS% -winsdk=%__VCVARSALL_WINSDK%"
)
if "%__VCVARSALL_STORE%" NEQ "" (
    set "__VCVARSALL_VSDEVCMD_ARGS=%__VCVARSALL_VSDEVCMD_ARGS% -app_platform=UWP"
)
if "%__VCVARSALL_VER%" NEQ "" (
    set "__VCVARSALL_VSDEVCMD_ARGS=%__VCVARSALL_VSDEVCMD_ARGS% -vcvars_ver=%__VCVARSALL_VER%"
)
if "%__VCVARSALL_SPECTRE%" NEQ "" (
    set "__VCVARSALL_VSDEVCMD_ARGS=%__VCVARSALL_VSDEVCMD_ARGS% -vcvars_spectre_libs=%__VCVARSALL_SPECTRE%"
)

goto :call_vsdevcmd

@REM -------------------------------------------------------------------------
@REM Call vsdevcmd.bat to setup the command prompt environment

:call_vsdevcmd

@REM This temporary environment variable is used to control setting of VC++
@REM command prompt-specific environment variables that should not be set
@REM by the VS Developer Command prompt (specifically vsdevcmd\ext\vcvars.bat).
@REM The known case this effects is the Platform environment variable, which
@REM will override platform target for .NET builds.
set VSCMD_VCVARSALL_INIT=1

@REM Special handling for the /clean_env argument
if "%__VCVARSALL_CLEAN_ENV%" NEQ "" (
    call "%~dp0..\..\..\Common7\Tools\vsdevcmd.bat" /clean_env
    goto :end
)

call "%~dp0..\..\..\Common7\Tools\vsdevcmd.bat" %__VCVARSALL_VSDEVCMD_ARGS%

if "%ERRORLEVEL%"=="0" (
    @REM Print the target command prompt architecture...
    if "%__VCVARSALL_HOST_ARCH%" NEQ "%__VCVARSALL_TARGET_ARCH%" (
        echo [%~nx0] Environment initialized for: '%__VCVARSALL_HOST_ARCH%_%__VCVARSALL_TARGET_ARCH%'
    ) else (
        echo [%~nx0] Environment initialized for: '%__VCVARSALL_TARGET_ARCH%'
    )
)
goto :end

:parse_loop
for /F "tokens=1,* delims= " %%a in ("%__VCVARSALL_ARGS_LIST%") do (
    if "%VSCMD_DEBUG%" GEQ "2" (
        @echo [DEBUG:%~nx0] inner argument {%%a}
    )
    call :parse_argument %%a
    set "__VCVARSALL_ARGS_LIST=%%b"
    goto :parse_loop
)

exit /B 0

:parse_argument

@REM called by :parse_loop and expects the arguments to either be:
@REM 1. a single argument in %1
@REM 2. an argument pair from the command line specified as '%1=%2'

set __local_ARG_FOUND=
@REM Architecture
if /I "%1"=="x86" (
    set __VCVARSALL_TARGET_ARCH=x86
    set __VCVARSALL_HOST_ARCH=x86
    set __local_ARG_FOUND=1
)
if /I "%1"=="x86_amd64" (
    set __VCVARSALL_TARGET_ARCH=x64
    set __VCVARSALL_HOST_ARCH=x86
    set __local_ARG_FOUND=1
)
if /I "%1"=="x86_x64" (
    set __VCVARSALL_TARGET_ARCH=x64
    set __VCVARSALL_HOST_ARCH=x86
    set __local_ARG_FOUND=1
)
if /I "%1"=="x86_arm" (
    set __VCVARSALL_TARGET_ARCH=arm
    set __VCVARSALL_HOST_ARCH=x86
    set __local_ARG_FOUND=1
)
if /I "%1"=="x86_arm64" (
    set __VCVARSALL_TARGET_ARCH=arm64
    set __VCVARSALL_HOST_ARCH=x86
    set __local_ARG_FOUND=1
)
if /I "%1"=="amd64" (
    set __VCVARSALL_TARGET_ARCH=x64
    set __VCVARSALL_HOST_ARCH=x64
    set __local_ARG_FOUND=1
)
if /I "%1"=="x64" (
    set __VCVARSALL_TARGET_ARCH=x64
    set __VCVARSALL_HOST_ARCH=x64
    set __local_ARG_FOUND=1
)
if /I "%1"=="amd64_x86" (
    set __VCVARSALL_TARGET_ARCH=x86
    set __VCVARSALL_HOST_ARCH=x64
    set __local_ARG_FOUND=1
)
if /I "%1"=="x64_x86" (
    set __VCVARSALL_TARGET_ARCH=x86
    set __VCVARSALL_HOST_ARCH=x64
    set __local_ARG_FOUND=1
)
if /I "%1"=="amd64_arm" (
    set __VCVARSALL_TARGET_ARCH=arm
    set __VCVARSALL_HOST_ARCH=x64
    set __local_ARG_FOUND=1
)
if /I "%1"=="x64_arm" (
    set __VCVARSALL_TARGET_ARCH=arm
    set __VCVARSALL_HOST_ARCH=x64
    set __local_ARG_FOUND=1
)
if /I "%1"=="amd64_arm64" (
    set __VCVARSALL_TARGET_ARCH=arm64
    set __VCVARSALL_HOST_ARCH=x64
    set __local_ARG_FOUND=1
)
if /I "%1"=="x64_arm64" (
    set __VCVARSALL_TARGET_ARCH=arm64
    set __VCVARSALL_HOST_ARCH=x64
    set __local_ARG_FOUND=1
)
if /I "%1"=="-vcvars_ver" (
    set "__VCVARSALL_VER=%2"
    set __local_ARG_FOUND=1
)
if /I "%1"=="/vcvars_ver" (
    set "__VCVARSALL_VER=%2"
    set __local_ARG_FOUND=1
)
if /I "%1"=="--vcvars_ver" (
    set "__vcvarsall_ver=%2"
    set __local_ARG_FOUND=1
)
if /I "%1"=="-vcvars_spectre_libs" (
    set "__VCVARSALL_SPECTRE=%2"
    set __local_ARG_FOUND=1
)
if /I "%1"=="/vcvars_spectre_libs" (
    set "__VCVARSALL_SPECTRE=%2"
    set __local_ARG_FOUND=1
)
if /I "%1"=="--vcvars_spectre_libs" (
    set "__vcvarsall_SPECTRE=%2"
    set __local_ARG_FOUND=1
)
if /I "%1"=="help" (
    set __VCVARSALL_HELP=1
    set __local_ARG_FOUND=1
)
if /I "%1"=="/help" (
    set __VCVARSALL_HELP=1
    set __local_ARG_FOUND=1
)
if /I "%1"=="-help" (
    set __VCVARSALL_HELP=1
    set __local_ARG_FOUND=1
)
if /I "%1"=="/?" (
    set __VCVARSALL_HELP=1
    set __local_ARG_FOUND=1
)
if /I "%1"=="-?" (
    set __VCVARSALL_HELP=1
    set __local_ARG_FOUND=1
)

@REM -- /clean_env --
@REM Mostly used for internal testing to restore the state of
@REM the command line environment to its state prior to vcvarsall.bat
@REM being executed.
if /I "%1"=="/clean_env" (
    set __VCVARSALL_CLEAN_ENV=/clean_env
    set __local_ARG_FOUND=1
)
if /I "%1"=="-clean_env" (
    set __VCVARSALL_CLEAN_ENV=/clean_env
    set __local_ARG_FOUND=1
)

@REM Windows SDK Version
if /I "%1"=="8.1" (
    set "__VCVARSALL_WINSDK=8.1"
    set __local_ARG_FOUND=1
)

set __temp1=%1
if /I "%__temp1:~0,3%"=="10." (
    set "__VCVARSALL_WINSDK=%1"
    set __local_ARG_FOUND=1
)
set __temp1=

@REM Store/UWP
if /I "%1"=="store" (
    set "__VCVARSALL_STORE=-app_platform=UWP"
    set __local_ARG_FOUND=1
)
if /I "%1"=="uwp" (
    set "__VCVARSALL_STORE=-app_platform=UWP"
    set __local_ARG_FOUND=1
)

if "%__local_ARG_FOUND%" NEQ "1" (
    set /A __VCVARSALL_PARSE_ERROR=__VCVARSALL_PARSE_ERROR+1
    if "%2"=="" (
        @echo [ERROR:%~nx0] Invalid argument found : %1
    ) else (
        @echo [ERROR:%~nx0] Invalid argument found : %1=%2
    )
)
set __local_ARG_FOUND=
exit /B 0

:usage_error
echo [ERROR:%~nx0] Error in script usage. The correct usage is:
goto :usage

:usage
echo Syntax:
echo     %~nx0 [arch] [platform_type] [winsdk_version] [-vcvars_ver=vc_version] [-vcvars_spectre_libs=spectre_mode]
echo where :
echo     [arch]: x86 ^| amd64 ^| x86_amd64 ^| x86_arm ^| x86_arm64 ^| amd64_x86 ^| amd64_arm ^| amd64_arm64
echo     [platform_type]: {empty} ^| store ^| uwp
echo     [winsdk_version] : full Windows 10 SDK number (e.g. 10.0.10240.0) or "8.1" to use the Windows 8.1 SDK.
echo     [vc_version] : {none} for latest installed VC++ compiler toolset ^|
echo                    "14.0" for VC++ 2015 Compiler Toolset ^|
echo                    "14.xx" for the latest 14.xx.yyyyy toolset installed (e.g. "14.11") ^|
echo                    "14.xx.yyyyy" for a specific full version number (e.g. "14.11.25503")
echo     [spectre_mode] : {none} for libraries without spectre mitigations ^|
echo                      "spectre" for libraries with spectre mitigations
echo:
echo The store parameter sets environment variables to support Universal Windows Platform application
echo development and is an alias for 'uwp'.
echo:
echo For example:
echo     %~nx0 x86_amd64
echo     %~nx0 x86_amd64 10.0.10240.0
echo     %~nx0 x86_arm uwp 10.0.10240.0
echo     %~nx0 x86_arm onecore 10.0.10240.0 -vcvars_ver=14.0
echo     %~nx0 x64 8.1
echo     %~nx0 x64 store 8.1
echo:
echo Please make sure either Visual Studio or C++ Build SKU is installed.
goto :end

:missing
echo The specified configuration type is missing.  The tools for the
echo configuration might not be installed.
goto :end

:end
set __VCVARSALL_TARGET_ARCH=
set __VCVARSALL_HOST_ARCH=
set __VCVARSALL_STORE=
set __VCVARSALL_WINSDK=
set __VCVARSALL_PARSE_ERROR=
set __VCVARSALL_CLEAN_ENV=
set VSCMD_VCVARSALL_INIT=
set __VCVARSALL_VSDEVCMD_ARGS=
set __VCVARSALL_HELP=
set __VCVARSALL_VER=
set __VCVARSALL_SPECTRE=

四、QtCreator设置MSVC编译套件分析

QtCreator对MSVC编译套件的识别,是通过Visual Studio的命令文件vcvarsall.bat来实现的。例如下图:

【Qt】一文总结QtCreator中MSVC编译套件_第4张图片

且每一种编译套件都会使用到该命令文件,然后将对应的参数传递给该批处理文件!

【Qt】一文总结QtCreator中MSVC编译套件_第5张图片

(注:小生安装了多个版本的Visual Studio,所以在QtCreator自动检测MSVC编译套件时识别到多种不同类型的编译套件)

上图中,每一项后面的括号中的内容,则代表了该种编译套件的运行宿主机和生成程序的目标平台。具体含义参见本文第二小节内容啦。例如:如果我们的平台是x86 32位宿主机,生成的代码同样需要运行在x86 32位的目标计算机上,故需要选择x86的编译套件。其他编译套件以此类推!


也可以在Visual Studio中搭建Qt的开发环境,但是小生还是比较喜欢QtCreator,工具而已,喜欢就好!haha

你可能感兴趣的:(小生聊【Qt】,qt,visual,studio,windows,MSVC,C++)