yann-qu

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

MSVC设置版本

在开发QT时,由于QT 5.12MSVC 2017兼容,因此需要用MSVC 2017来编译使用QT 5.12的程序。

1 安装MSVC 2017

由于笔者电脑上安装的Visual Studio版本为Visual Studio 2022,缺少MSVC 2017,因此需要先安装MSVC 2017

首先打开Visual Studio Installer,对以安装的Visual Studio程序点击修改,在单个组件中搜索msvc。在搜索结果中勾选MSVC v141 - VS 2017 C++ x64/x86生成工具(v14.16),并进行安装即可。

2 设置MSVC版本

设置MSVC版本需要使用脚本vcvarsall.bat进行,此脚本的路径一般为:C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat,此脚本的原理是该脚本会配置MSVC相关的一些环境变量。

cmd中执行如下命令:

> .\vcvarsall.bat

会输出脚本的使用方法:

Syntax:
    vcvarsall.bat [arch] [platform_type] [winsdk_version] [-vcvars_ver=vc_version] [-vcvars_spectre_libs=spectre_mode]
where :
    [arch]: x86 | amd64 | x86_amd64 | x86_arm | x86_arm64 | amd64_x86 | amd64_arm | amd64_arm64
    [platform_type]: {empty} | store | uwp
    [winsdk_version] : full Windows 10 SDK number (e.g. 10.0.10240.0) or "8.1" to use the Windows 8.1 SDK.
    [vc_version] : {none} for latest installed VC++ compiler toolset |
                   "14.0" for VC++ 2015 Compiler Toolset |
                   "14.xx" for the latest 14.xx.yyyyy toolset installed (e.g. "14.11") |
                   "14.xx.yyyyy" for a specific full version number (e.g. "14.11.25503")
    [spectre_mode] : {none} for libraries without spectre mitigations |
                     "spectre" for libraries with spectre mitigations

The store parameter sets environment variables to support Universal Windows Platform application
development and is an alias for 'uwp'.

For example:
    vcvarsall.bat x86_amd64
    vcvarsall.bat x86_amd64 10.0.10240.0
    vcvarsall.bat x86_arm uwp 10.0.10240.0
    vcvarsall.bat x86_arm onecore 10.0.10240.0 -vcvars_ver=14.0
    vcvarsall.bat x64 8.1
    vcvarsall.bat x64 store 8.1

Please make sure either Visual Studio or C++ Build SKU is installed.

注意:

脚本中所说明的Windows SDK版本和MSVC版本均需要结合自己电脑来填写,以下示例仅为笔者电脑中的版本号,不一定适用于所有读者。请仔细阅读脚本说明。Windows SDK版本号可以在已安装程序列表中查看,本例中为10.0.22000.0;MSVC版本号可以在Visual Studio Installer安装组件时勾选的组件后面的小括号中有注明,本例中为14.16。

MSVC编译器的安装路径为C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC,此路径下的文件夹名也代表MSVC编译器的版本。笔者在此目录下有两个文件夹分别为14.16.2702314.34.31933,说明笔者安装了14.1614.34两个版本的MSVC编译器。

仔细阅读说明后,我们可以使用如下命令为MSVC编译器设置版本:

> call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64 10.0.22000.0 -vcvars_ver=14.16

然后在命令行中调用MSVC编译器查看其版本:

> cl.exe

输出信息为

Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27048 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]

同理,我们可以使用如下设置命令将MSVC编译器设置为另一个版本:

> call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64 10.0.22000.0 -vcvars_ver=14.16

然后在命令行中调用MSVC编译器查看其版本是否发生变化:

> cl.exe

输出信息为

Microsoft (R) C/C++ Optimizing Compiler Version 19.34.31937 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]

对比发现MSVC编译器的版本确实发生了变化。

注意:

必须要在cmd使用以上命令调用脚本,在powershell中无效。

3 实际应用

在实际应用中,我们需要先打开cmd,使用以上命令调用脚本设置合适的编译器版本后,再从cmd中启动IDE(如clion、vscode),再进行开发即可。

在cmake config时会输出MSVC版本号,我们可以确认是否设置正确。

此外,可以用以下代码输出MSVC版本号:

#include <iostream>
int main(int argc, char *argv[]) {
    std::cout << _MSC_VER << std::endl;  // 此例中会输出1916或1934
    return 0;
}
posted on 2023-02-05 18:45  yann-qu  阅读(1117)  评论(0编辑  收藏  举报