Build Tools for Visual Studio 2022 (ビルドツール for Visual Studio 2022)または Visual Studio 2022 のインストール(Windows 上)

Build Tools for Visual Studio

Build Tools for Visual Studio は,Visual Studio の IDE を含まない C/C++ コンパイラ,ライブラリ,ビルドツール等のコマンドライン向け開発ツールセットである。

Visual Studio

Visual Studio は統合開発環境であり,Build Tools for Visual Studio と連携して使用する。

Build Tools for Visual Studio 2022 のインストール(Windows 上)

以下のコマンドを管理者権限コマンドプロンプトで実行する (手順:Windowsキーまたはスタートメニュー → cmd と入力 → 右クリック → 「管理者として実行」)。

REM VC++ ランタイム
winget install --scope machine --accept-source-agreements --accept-package-agreements --silent --id Microsoft.VCRedist.2015+.x64

REM Build Tools + Desktop development with C++(VCTools)+ 追加コンポーネント(一括)
winget install --id Microsoft.VisualStudio.2022.BuildTools ^
  --override "--passive --wait --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended --add Microsoft.VisualStudio.Component.VC.Llvm.Clang --add Microsoft.VisualStudio.ComponentGroup.ClangCL --add Microsoft.VisualStudio.Component.VC.CMake.Project --add Microsoft.VisualStudio.Component.Windows11SDK.26100"

--add で追加されるコンポーネント

上記のコマンドでは,まず Build Tools 本体と Visual C++ 再頒布可能パッケージをインストールし,次に setup.exe を用いて以下のコンポーネントを追加している。

インストール完了の確認

winget list Microsoft.VisualStudio.2022.BuildTools

上記以外の追加のコンポーネントが必要になった場合は Visual Studio Installer で個別にインストールできる。

Visual Studio Community 2022 の追加インストール(Windows 上)

Build Tools では対応できないケースもある。

その場合は Build Tools の環境に Visual Studio Community 2022 を追加インストールできる。

以下のコマンドの実行前に、Build Tools for Visual Studio 2022 のインストールを終えておくこと。

以下のコマンドを管理者権限コマンドプロンプトで実行する (手順:Windowsキーまたはスタートメニュー → cmd と入力 → 右クリック → 「管理者として実行」)。

winget install --scope machine --accept-source-agreements --accept-package-agreements Microsoft.VisualStudio.2022.Community --override "--quiet --add Microsoft.VisualStudio.Workload.NativeDesktop Microsoft.VisualStudio.ComponentGroup.NativeDesktop.Core Microsoft.VisualStudio.Component.VC.CLI.Support Microsoft.VisualStudio.Component.CoreEditor Microsoft.VisualStudio.Component.NuGet Microsoft.VisualStudio.Component.Roslyn.Compiler Microsoft.VisualStudio.Component.TextTemplating Microsoft.VisualStudio.Component.Windows11SDK.26100 Microsoft.VisualStudio.Component.VC.Tools.x86.x64 Microsoft.VisualStudio.Component.VC.ATL Microsoft.VisualStudio.Component.VC.ATLMFC Microsoft.VisualStudio.Component.VC.Llvm.Clang Microsoft.VisualStudio.Component.VC.Llvm.ClangToolset Microsoft.VisualStudio.Component.VC.CMake.Project Microsoft.VisualStudio.Component.VC.ASAN Microsoft.VisualStudio.Component.Vcpkg"

Visual Studio Community は無償だが、企業利用にはライセンス条件(個人開発者、オープンソース開発、学術研究、小規模組織に限定)があり、条件を満たさない場合は Professional 以上のエディションが必要となる。

コマンドで C プログラムをコンパイルしてみる

  1. 以下の操作をx64 Native Tools コマンドプロンプト (x64 Native Tools Command Prompt)で実行する   (手順:スタートメニュー →」の下の「x64 Native Tools コマンドプロンプト (x64 Native Tools Command Prompt)」を選ぶ)。
  2. cl にパスが通っていることを確認する

    エラーメッセージが出ていないことを確認.

    where cl
    

    *cl が無い」 という場合は,次の手順で,Visual Studio Build Tools 2022C++ についての設定を行う.
    1. C++ についての設定をしたいので、Visual Studio Installer (Visual Studio インストーラー)を起動

      Windows のスタートメニューからの起動が簡単

    2. Visual Studio Build Tools 2022 の画面で「変更」をクリック
    3. C++ によるデスクトップ開発」をチェック.そして,画面右側の「インストール」の詳細で「v143 ビルドツール用 C++/CLI サポート(最新)」をチェックする.その後,「インストール」をクリック.
    4. インストールが始まる.しばらく待つ.
  3. コンパイラの動作確認

    まず,エディタを開く. ここではメモ帳 (notepad) を使っている.

    次のコマンドをx64 Native Tools コマンドプロンプト (x64 Native Tools Command Prompt)で実行する   (手順:スタートメニュー →」の下の「x64 Native Tools コマンドプロンプト (x64 Native Tools Command Prompt)」を選ぶ)。 ファイル名は hello.c としている.

    cd /d c:%HOMEPATH%
    notepad hello.c
    
  4. いまメモ帳で開いたファイルを, 次のように編集する(コピー&ペーストしてください).そして保存する.
    #include<stdio.h>
    int main() {
        printf("Hello,World!\n");
        printf("sizeof(size_t)=%ld\n", sizeof(size_t));
        return 0;
    }
    
  5. 次のコマンドをx64 Native Tools コマンドプロンプト (x64 Native Tools Command Prompt)で実行する   (手順:スタートメニュー →」の下の「x64 Native Tools コマンドプロンプト (x64 Native Tools Command Prompt)」を選ぶ)。

    結果として,「Hello,World!」「sizeof(size_t)=8」と表示されればOK.

    del hello.exe
    cl hello.c
    .\hello.exe
    

    実行結果例