MFC アプリ開発環境構築 Visual Studio 2022 版

Visual Studio 2022 がリリースされました。

visualstudio.microsoft.com

2019 以来のメジャーバージョンアップ。ずっと奇数年リリースでしたが、コロナ禍の影響か 2021 ではなく 2022 になりましたね。

blog.kondoumh.com

それでは、レガシーな MFC アプリである iEdit のビルド環境を構築していきます。

Community Edition のインストーラーをダウンロードして起動

f:id:kondoumh:20211210165014p:plain

Visual Studio Installer がインストールされます。

f:id:kondoumh:20211210165112p:plain

Visual Studio Installer の画面で「C++ によるデスクトップ開発」にチェックを入れます。

f:id:kondoumh:20211210165148p:plain

2019 の時のブログを参考にインストールオプションを選びました。2019 は .NET 開発とかも入れてましたが、当面使う予定ないので C++ だけに絞りました*1。それでも16GBもあります。

f:id:kondoumh:20211210165200p:plain

f:id:kondoumh:20211210165220p:plain

インストールが完了。

f:id:kondoumh:20211210165232p:plain

Community Edition は Microsoft アカウントにサインインした状態でないと使えません。

f:id:kondoumh:20211210165257p:plain

サインインすると起動しました。インストーラーは OS のダークテーマに同期していましたが、Visual Studio はデフォルトのテーマになっています。

f:id:kondoumh:20211210165310p:plain

iEdit のソリューションを開きます。

f:id:kondoumh:20211210165323p:plain

ソリューションファイルは Visual Studio 2019 で使ったものなので、「ソリューション操作の再ターゲット」ダイアログが開きました。10 の最新版に設定しました。

f:id:kondoumh:20211210165334p:plain

再ターゲットが終わると、vcxproj ファイルに差分が発生し、PlatformToolset が v142 から v143 に上がりました。

f:id:kondoumh:20211210165345p:plain

一旦開きましたが、iEdit の JSON 処理は C++ REST SDK という Microsoft 製の OSS に依存しているため、この環境も作る必要があります。

blog.kondoumh.com

ということで過去ブログを参考に C++ REST SDK のスタティックリンクができるよう、構築していきます。

まず vcpkg のリポジトリを clone します。

gh repo clone microsoft/vcpkg

powrshell で以下を実行し vcpkg.exe を作ります。

.\vcpkg\bootstrap-vcpkg.bat

vcpkg ディレクトリを vcpkg の root に設定します。

> .\vcpkg.exe integrate install
Applied user-wide integration for this vcpkg root.

All MSBuild C++ projects can now #include any installed libraries.
Linking will be handled automatically.
Installing new libraries will make them instantly available.

cpprestsdk の static 版をインストールしようとするとエラーになりました。

> .\vcpkg.exe install cpprestsdk:x86-windows-static
Computing installation plan...
The following packages will be built and installed:
  * brotli[core]:x86-windows-static -> 1.0.9#2
    cpprestsdk[brotli,compression,core,default-features]:x86-windows-static -> 2.10.18
  * zlib[core]:x86-windows-static -> 1.2.11#13
Additional packages (*) will be modified to complete this operation.
Error: in triplet x86-windows-static: Unable to find a valid Visual Studio instance
The following VS instances were excluded because the English language pack is unavailable:
    C:\Program Files\Microsoft Visual Studio\2022\Community
    C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools
Could not locate a complete Visual Studio instance
The following paths were examined for Visual Studio instances:
    C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary/Build\vcvarsall.bat
    C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools/MSVC\14.30.30705\bin/HostX86/x86\dumpbin.exe
    C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary/Build\vcvarsall.bat
    C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools/MSVC\14.29.30133\bin/HostX86/x86\dumpbin.exe

どうも Language pack に英語もインストールしておかなくてはいけないようです。

Visual Studio Installer を起動し変更ボタンをクリック。

f:id:kondoumh:20211211145125p:plain

言語パックで「英語」にチェックを入れ変更ボタンをクリックして language pack を追加します。

f:id:kondoumh:20211211145216p:plain

この状態で vcpkg install をリトライするとインストールできました。

> .\vcpkg.exe install cpprestsdk:x86-windows-static
Computing installation plan...
The following packages will be built and installed:
  * brotli[core]:x86-windows-static -> 1.0.9#2
    cpprestsdk[brotli,compression,core,default-features]:x86-windows-static -> 2.10.18
  * zlib[core]:x86-windows-static -> 1.2.11#13
Additional packages (*) will be modified to complete this operation.
Detecting compiler hash for triplet x86-windows-static...
A suitable version of powershell-core was not found (required v7.2.0). Downloading portable powershell-core v7.2.0...
Downloading powershell-core...
  https://github.com/PowerShell/PowerShell/releases/download/v7.2.0/PowerShell-7.2.0-win-x86.zip -> C:\Users\kondoh\dev\vcpkg\downloads\PowerShell-7.2.0-win-x86.zip
Extracting powershell-core...
  :
-- Building x86-windows-static-dbg
-- Building x86-windows-static-rel
-- Installing: C:/Users/kondoh/dev/vcpkg/packages/cpprestsdk_x86-windows-static/share/cpprestsdk/copyright
-- Performing post-build validation
-- Performing post-build validation done
Stored binary cache: C:\Users\kondoh\AppData\Local\vcpkg\archives\8c\8cc3a921835f34a2df04faf83ca4954562138ed1e44509944ff13f1219ffe554.zip
Installing package cpprestsdk[brotli,compression,core,default-features]:x86-windows-static...
Elapsed time for package cpprestsdk:x86-windows-static: 30.72 s

Total elapsed time: 1.301 min

The package cpprestsdk provides CMake targets:

    find_package(cpprestsdk CONFIG REQUIRED)
    target_link_libraries(main PRIVATE cpprestsdk::cpprest cpprestsdk::cpprestsdk_zlib_internal cpprestsdk::cpprestsdk_brotli_internal)

iEdit のプロジェクトの設定は過去ブログのままでビルドできました。

自プロジェクト側の構成プロパティ設定です。

vcpkg で OSS のライブラリを導入し Visual C++ プロジェクトに静的リンクする - kondoumh のブログ

これで、Windows 11 にアップデートしてもビルド大丈夫そうです。

次は GitHub Actions で Windows Runner 使ってビルドする CI を作りたいところです。

*1:.NET Core なら VS Code でも開発できますし。