はじめに
お久しぶりで.前回の記事からだいぶ間が空いてしまったのでリハビリがてらC/C++の環境を構築したいと思います.
毎回WindowsをクリーンインストールするとC/C++環境構築のやり方を調べなくてはいけないので備忘録として緩めに残しておきます.
なお,今回はwinget及びVSCodeの環境は整っていることを前提としています.
clangのインストール
PowerShellで以下の通りclangをインストールしていきます.
winget install LLVM.LLVM
続いて,Pathを以下のコマンドで通していきます.
[Environment]::SetEnvironmentVariable("PATH", $env:PATH + ";C:\Program Files\LLVM\bin", "User")
以上でclangはOKです.ターミナルを再起動し以下のようになればOKです.
clang --version
clang version 21.1.1
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
VSCode
まずはCode Runner君を入れてあげます.これを使うことで,様々な言語を即座に実行できる便利拡張機能です(すでに入っている人が多いかも?)

続いて,Code Runnerの設定です.setting jsonを開き以下を追加します.
"code-runner.runInTerminal": true,
"code-runner.executorMap": {
"javascript": "node",
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
"c": "cd $dir && clang $fileName -o $fileNameWithoutExt.exe && $dir$fileNameWithoutExt",
"zig": "zig run",
"cpp": "cd $dir && clang++ $fileName -o $fileNameWithoutExt.exe && $dir$fileNameWithoutExt",
"objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"php": "php",
"python": "python -u",
"perl": "perl",
"perl6": "perl6",
"ruby": "ruby",
"go": "go run",
"lua": "lua",
"groovy": "groovy",
"powershell": "powershell -ExecutionPolicy ByPass -File",
"bat": "cmd /c",
"shellscript": "bash",
"fsharp": "fsi",
"csharp": "scriptcs",
"vbscript": "cscript //Nologo",
"typescript": "ts-node",
"coffeescript": "coffee",
"scala": "scala",
"swift": "swift",
"julia": "julia",
"crystal": "crystal",
"ocaml": "ocaml",
"r": "Rscript",
"applescript": "osascript",
"clojure": "lein exec",
"haxe": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
"rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",
"racket": "racket",
"scheme": "csi -script",
"ahk": "autohotkey",
"autoit": "autoit3",
"dart": "dart",
"pascal": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
"d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
"haskell": "runghc",
"nim": "nim compile --verbosity:0 --hints:off --run",
"lisp": "sbcl --script",
"kit": "kitc --run",
"v": "v run",
"sass": "sass --style expanded",
"scss": "scss --style expanded",
"less": "cd $dir && lessc $fileName $fileNameWithoutExt.css",
"FortranFreeForm": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"fortran-modern": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"fortran_fixed-form": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"fortran": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"sml": "cd $dir && sml $fileName",
"mojo": "mojo run",
"erlang": "escript",
"spwn": "spwn build",
"pkl": "cd $dir && pkl eval -f yaml $fileName -o $fileNameWithoutExt.yaml",
"gleam": "gleam run -m $fileNameWithoutExt"
}
一行目のrunInTerminalはコマンドをターミナルで実行してあげるよっていう設定です.これをしないと標準入力が使えないという問題が発生します(なんでデフォでtrueじゃないんや?).
二行目のexecutorMapは言語ごとの実行コマンドを設定する事ができます.デフォルトだとgcc/g++になっていたりするので,書き換えてあげます.書き換えたのはc,cppの部分のみなので,この部分だけをコピペすれば大丈夫です.
あとは,C/C++ Extension Packを入れて完了です.

実行
あとは以下のような適当なC/C++コードを作成して,実行します.
#include <iostream>
int main()
{
std::cout<<"hello clang"<<std::endl;
}
VSCode上でCtrl+Alt+NでCode Runner君が実行してくれます.
終わりに
今回はC/C++の環境構築をしてみました.
いやーやっぱりwinget君が便利ですね.GCCはまだWingetじゃ入れられなさそう(当社調べ)なので,これからはよっぽどのことがない限りClangしか使わなくなりそうですね.
コメント