LoginSignup
1
4

C言語環境構築 for Mac

Last updated at Posted at 2023-09-10

目次

1.はじめに
2.VS Codeのインストール
3.gccのインストール
4.Code Runnerの設定
5.おわりに

1. はじめに

 Windowsの場合,WSLなどを使って比較的簡単に環境構築を行うことが出来ます.Macの場合,XCodeから導入することが出来るgccが存在しますが,gccという名前がついてるだけで実体はclangです.

❯ /usr/bin/gcc --version
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: x86_64-apple-darwin22.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

今回は,Macで本物のgccを使うためのセットアップを紹介します.
私の環境は以下の通りです.

❯ sw_vers 
ProductName:		macOS
ProductVersion:		13.5.2
BuildVersion:		22G91

また,shellはzshを使用しています.

2. VS Codeのインストール

 ここからVS Codeをダウンロードしましょう.

3. gccのインストール

 Homebrewで一発でインストール出来ます.

brew install gcc

終わったら,~/.zshrcに以下を書き込みましょう.

~/.zshrc
#GCC
alias gcc='/usr/local/bin/gcc-*'
alias g++='/usr/local/bin/g++-*'

source ~/.zshrcをしたのち,次のようになっていたら,うまく設定されています.

❯ gcc --version
gcc-13 (Homebrew GCC 13.1.0) 13.1.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

4. Code Runnerの設定

拡張機能「Code Runner」をVS Codeにインストールしてから,settings.jsonに以下を追記してください.
 Cの部分だけ抜き出すのも面倒なので,私の設定を全て書いておきます.これをそのままVS Codeのsettings.jsonに追記してください.
これで大抵のプログラムは「⌥⌘N」で実行できるようになります.

settings.json
"code-runner.runInTerminal": true,
  "code-runner.executorMap": {
    "awk": "awk -f $fileName",
    "javascript": "node",
    "java": "cd $dir && javac \"$fileName\" && java \"$fileNameWithoutExt\"",
    "c": "cd $dir && gcc -fexec-charset=UTF-8 \"$fileName\" -o \"$fileNameWithoutExt\" && $dir\"$fileNameWithoutExt\"",
    "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
    "objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
    "php": "php",
    "python": "cd $dir && python \"$fileName\"",
    "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": "flutter run -d all",
    "pascal": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
    "d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
    "haskell": "runhaskell",
    "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"
  }
}

5. おわりに

よきgcc Lifeを!

1
4
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
1
4