われがわログ

最適化アルゴリズムとかプログラミングについて書きたい

Visual StudioのDeveloper ShellをPowerShellのコマンドラインからactivateする

WindowsでCMake & MSVCを使用する際はいつもスタートメニューから「Developer PowerShell for VS 2019」を起動していたが、いい加減面倒になって、コマンドラインから起動する方法を調べたのでメモしておく。

結論としては、profile.ps1(場所は$profile で見られる)に以下の設定をすれば、以降はcondaのenvironmentのようにactivate_msvcで起動できる。

Set-Alias activate_msvc activate_msvc_impl

function activate_msvc_impl {
    $installPath = &"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -version 16.0 -property installationpath
# - SkipAutomaticLocation を指定しないと勝手にcdされる
# -DevCmdArguments -arch=x64 を指定するとx64コンパイラが使用できる
    Import-Module (Join-Path $installPath "Common7\Tools\Microsoft.VisualStudio.DevShell.dll") && Enter-VsDevShell -VsInstallPath $installPath -SkipAutomaticLocation
}

Set-Alias activate_msvc activate_msvc_impl

参考