LoginSignup
2
1

Macで簡単なC#ファイルのコンパイルと実行をVSCode上で自動化する方法

Last updated at Posted at 2023-10-12

背景

C# は通常Visual Studio(Windowsの場合)やVisual Studio for Mac(Macなどの場合)上で開発・実行されますが,サンプルコードを動かしてみたり,競技プログラミングでテストをしてみたりするときなど,簡単なソースコードをすぐに実行したいことがあります.実はC#は先述したような統合開発環境(IDE)を使用しないでも,ターミナルからコマンドでコンパイルし,実行することが可能です.私の大好きな後輩が競技プログラミングを始めるに際してC#の実行方法について悩んでいたので,ここではVSCodeの拡張機能であるCode Runnerを使用して,コマンドひとつでC#のコンパイルおよび実行を行えるようにする手順を紹介したいと思います.

C#のコンパイルと実行の方法

Windowsの場合,.NET Frameworkと呼ばれるフレームワークの中に,C#のコンパイラであるcsc.exeが標準装備されています.しかし,この.NET FrameworkはMicrosoft社がWindows上でのみ利用することを想定して開発したものであるため,Windows以外のOSでは使えません.そこで,ここではクロスプラットフォームの.NET実行環境であるMonoを使用します.MonoはMacやLinux系OSで使うことができます.

公式サイト

Monoについて

Sponsored by Microsoft, Mono is an open source implementation of Microsoft's .NET Framework as part of the .NET Foundation and based on the ECMA standards for C# and the Common Language Runtime. A growing family of solutions and an active and enthusiastic contributing community is helping position Mono to become the leading choice for development of cross platform applications.
(https://www.mono-project.com/より)

Monoをインストールする

Homebrewからインストールするのが良いでしょう.ターミナルで以下を実行します.

terminal
brew install mono

C#を実行してみる

サンプルコード

以下はHello World!を出力するサンプルプログラムです.

hello.cs
using System;

public class HelloWorld {
    static public void Main() {
        Console.WriteLine("Hello World!");
    }
}

試しに,上記プログラムをコンパイルして,実行してみましょう.先ほど説明したように.NET Frameworkではcscコマンドを用いることができますが,Monoでは代わりにmcs(Mono C# Compiler) コマンドを使います.以下をコンパイルしたいファイルのあるディレクトリの上で叩いてみましょう.

terminal
mcs hello.cs

エラーを吐かず,hello.exeというファイルが生成されていれば,成功でしょう.

次に実行です.実行にはmono コマンドを使います.以下をターミナルで叩いてください.

terminal
mono hello.exe

結果は以下のようになります.

terminal
Hello World!

無事実行できたでしょうか.以上がコンパイルから実行までの流れになります.まとめると,

  1. cscコマンドでコンパイルする.
  2. monoコマンドで実行する.

という流れです.

VSCodeで自動化する

コンパイルと実行の流れはそこそこ長いコマンドを叩かなければならないため,少々面倒です.VSCodeでは Code Runner というコマンドをショートカットひとつで打ち込んでくれる拡張機能が存在します.これを使いましょう.

VSCodeのインストール

VSCodeをインストールしていない方は,以下よりインストールしてください.

Code Runnerのインストール

VSCodeの拡張機能をタブ(下記画像部分)を開いて,"Code Runner"と検索し,インストールしてください.
スクリーンショット 2023-10-13 0.20.30.jpg

ついでにここで,C#の拡張機能をインストールしても良いでしょう.
image.png

コマンドをカスタマイズ

Code Runnerはcommand + control + Nのショートカットで,ターミナルにコードのプログラミング言語に応じたコンパイル&実行のコマンドを打ち込んでくれます.打ち込んでくれるコードは,defaultSettings.json内で定義されています.

defaultSettings.json
"code-runner.executorMap": {
	"javascript": "node",
	"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
	"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
	"zig": "zig run",
	"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $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"
}

このファイルでは,プログラミング言語の種類と,ショートカットキーを押したときにターミナルに打ち込まれるコマンドの対応関係を示しています.C#の部分を見てみると,"csharp": "scriptcs",となっており,これはmonoによるコンパイル&実行コマンドと一致していません.ですので,User SettingsのJSONにmonoのコマンドを打ち込むような設定を上書きしてしまいましょう.setting.jsonは,command + shift + pでコマンドパレットを開き,画像のように,

Open User Settings

と打ち込んで開くことができます.

image.png

開くことができたら以下を追記してください.記述する場所はどこでもいいです.

:settings.json
"code-runner.executorMap": {
  "csharp": "mcs $fileName && mono $fileNameWithoutExt.exe ",
}

前後のオブジェクトとの間にカンマを忘れずに!

ショートカットでコンパイル&実行してみる

いよいよショートカットによるコンパイル&実行です.hello.csに戻って,command + control + Nを押下してください.うまくいけば,以下のように実行されるでしょう.

terminal
% mcs hello.cs && mono hello.exe
Hello World!

ここまでできたら完了です.

まとめ

以上の手順により,VSCode上でC#プログラムのショートカットによる自動コンパイル&実行ができるようになりました.これで僕の後輩もより一層C#の勉強に精を出してくれることでしょう.今度お礼でもしてください.

参考サイト

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1