0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C言語 VisualStudioCode 環境構築 [F5]キーでビルド、実行

Last updated at Posted at 2025-09-04

mingw-w64のダウンロード

https://www.mingw-w64.org/

[Pre-built Toolchains]を選択する
スクリーンショット 2025-09-04 150348.png

[MinGW-W64-builds]を選択する
スクリーンショット 2025-09-04 151039.png

[GitHub]を選択する
スクリーンショット 2025-09-04 152454.png

[x86_64-15.2.0-release-win32-seh-msvcrt-rt_v13-rev0.7z]を選択する
Windowsの場合は、ファイル名に、[x86-64]、 [win32]、[msvcrt]があるファイルを選択する
スクリーンショット 2025-09-04 152729.png

mingw-w64のインストール

ダウンロードしたファイルを解凍する
解凍してできたフォルダ内にある、[mingw64]フォルダを、Cドライブ直下へ移動する

環境変数の設定

C:\mingw64\binを環境変数に設定する
https://dohi.chiba.dendai.ac.jp/~dohi/tdupress/c/hp/environment/windows-11/mingw-install-windows.pdf

Visual Studio Codeに拡張機能をインストールする

[C/C++ Extension Pack]をインストールする

開発用フォルダ作成

任意のフォルダを作成して、Visual Studio Codeで開く
例:[test]フォルダ

サンプルとして以下のファイルを作成する

sample.c
#include <stdio.h>

int main(void)
{
   printf("あいうえお\n");
   return 0;
}

tasks.jsonファイル作成

Visual Studio Codeで、[表示]-[コマンド パレット]
表示されたメニューから、[タスク:タスクの構成]を選択する
taskconfig.png

次に表示されたメニューから、[C/C++: gcc.exe アクティブなファイルのビルド]を選択する
taskconfig2.png

[.vscode]フォルダが作成されて、その内にtasks.jsonファイルが作成される
スクリーンショット 2025-09-04 160615.png

tasks.json
{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "C/C++: gcc.exe アクティブなファイルのビルド",
			"command": "C:\\mingw64\\bin\\gcc.exe",
			"args": [
				"-fdiagnostics-color=always",
				"-g",
				"${file}",
				"-o",
				"${fileDirname}\\${fileBasenameNoExtension}.exe"
			],
			"options": {
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": "build",
			"detail": "コンパイラ: C:\\mingw64\\bin\\gcc.exe"
		}
	]
}

launch.jsonファイル作成

[実行とデバック]ボタン - [launch.jsonファイルを作成します。]
表示されたメニューから、[C++(GDB/LLDB)]を選択する
launch1.png

表示されたメニューから、[{}C/C++: (gdb) 起動]を選択する
launch2.png

作成されたlaunch.jsonを編集する

  • "program"の箇所を変更
  • "miDebuggerPath"の箇所を変更
  • "preLaunchTask"の箇所を追加
    "preLaunchTask"は、tasks.jsonの"label"と同じ文字列を設定する
    同じにしないとビルドされない

編集したlaunch.jsonファイル

launch.json
{
    // IntelliSense を使用して利用可能な属性を学べます。
    // 既存の属性の説明をホバーして表示します。
    // 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 起動",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "gdb の再フォーマットを有効にする",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "逆アセンブリ フレーバーを Intel に設定",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            // tasks.jsonの"label"と同じ文字列を設定する
            "preLaunchTask": "C/C++: gcc.exe アクティブなファイルのビルド"
        }
    ]
}

[F5]キーで、ビルド、実行

Visual Studio Codeで、C言語のソースコードsample.cタブを選択し、[F5]キーを押すと、ビルド、実行される

C言語のソースコードではないタブを選択した状態で[F5]キーを押しても、ビルド、実行はされない

例えばlaunch.jsonタブを選択した状態で[F5]キーを押すと、下図のエラーが表示される
スクリーンショット 2025-09-04 164354.png

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?