使用するC言語開発環境
C言語の開発環境は
-
コンパイラ
-
エディタ
から構成される.
コンパイラ (Compiler)
本講義では,C言語コンパイラとして clang を用いる.clangはオープンソースのLLVM(任意のプログラミング言語に対応可能なコンパイラ基盤)プロジェクトのC言語向けのフロントエンドである.
C言語のコンパイラとして,他には gccや Visual C++などが存在する.
エディタ (Editor)
C言語を記述するためには,キーボードからの文字入力をファイルとして保存するためにエディタと呼ばれるソフトウェアが必要である.エディタには簡素なものから,高機能なものまで様々な選択肢が存在する.本講義では Visual Studio Code (以下vscodeと略記)を用いる.
Visual Studio Code
vscodeでは,拡張機能をインストールすることで,様々な言語に対応したソースコード記述が可能である.拡張機能は Shift+Ctrl+X (macOSではShift+Cmd+X)を押下することで検索およびインストールができる.
本講義では,最低限,以下の機能拡張がインストールされていることを前提とする.
-
C/C++
-
CodeLLDB
また,前提とするディレクトリ構成は以下のとおりである.
📒 H: (1)
📂 Documents (2)
📂 Progs2(3)
📁 .vscode (4)
📄 repo01.c (5)
📂 Progs (6)
📁 .vscode (7)
📄 repo01.c (8)
| 1 | サーバ上のホームドライブ |
かならずHドライブからたどって,Progs2フォルダを右クリックし を選択し,フォルダを開くこと.さもないとコンパイル時やデバッグ時にエラーメッセージが表示されることがある.
|
.vscode フォルダの中身には以下の`settings.json`,`tasks.json`および`launch.json`の3つのファイルを配置する.
| これらの設定ファイルは,中身を理解できないうちはいじらないこと. |
リスト 1. settings.json
{
"terminal.integrated.defaultProfile.windows": "PowerShell",
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"args": [
"-NoExit",
"-Command",
"chcp 65001"
],
}
}
}
リスト 2. tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang.exe build active file",
"command": "C:\\tools\\msys64\\clang64\\bin\\clang.exe",
"args": [
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:\\tools\\msys64\\clang64\\bin\\clang.exe"
}
]
}
リスト 3. launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"cwd": "${workspaceFolder}",
"preLaunchTask": "C/C++: clang.exe build active file"
}
]
}