1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

VSCode C++環境設定(Windows Mingw-w64)

Posted at

概要

Visual Studio Codeで、C++ビルド・デバッグ環境を整備してみました。
まずはWindows 10でMingw-w64(8.1.0)の環境を作ってみましょう。

# 環境 バージョン 備考
1 Windows 10 Pro 20H2
2 Visual Studio Code 1.55.2 + C/C++ for Visual Studio Code
3 Mingw-w64 8.1.0 http://mingw-w64.org/

設定ファイル一覧

設定ファイルは、ワークスペースの直下にある.vscodeフォルダにあります。

~/.vscode/launch.json

デバッガー(gdb.exe)へのパスを"miDebuggerPath"に指定します。

launch.json
{
    "version": "0.2.0",
    "configurations": [
    {
        "name": "g++.exe - アクティブ ファイルのビルドとデバッグ",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "miDebuggerPath": "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gdb.exe",
        "setupCommands": [
            {
                "description": "gdb の再フォーマットを有効にする",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ],
        "preLaunchTask": "[Windows] C/C++: g++.exe アクティブなファイルのビルド"
    }
    ]
}

~/.vscode/tasks.json

大きなスタックサイズを確保して、ビルドされたロードモジュールを起動すると、SIGSEGV(セグメンテーション障害)でダウンしてしまうので、スタックサイズを大きく取ります(デフォルトは8メガバイトのようです)。
g++には、-Wl,--stack,<size>の指定をするのですが、以下のように二つのパラメーターに分けて指定をしないといけないようです。
なお、この設定では、64メガバイトのスタック領域を確保しています。
ちなみに、Linux g++でサポートされている-fsplit-stackは、Windowsではサポートされておらず、g++.exeがエラーになってしまいました。

tasks.json
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "[Windows] C/C++: g++.exe アクティブなファイルのビルド",
            "command": "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/g++.exe",
            "args": [
                "-Wl,--stack",
                "-Wl,67108864",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "デバッガーによって生成されたタスク。"
        }
    ],
    "version": "2.0.0"
}

~/.vscode/c_cpp_properties.json

このくらいincludePathを指定しておけばよいでしょうか?(かなりパスが深いです)
全ヘッダ取り込みの、#include <bits/stdc++.h>の指定も使えるようになります。

c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/include",
                "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/x86_64-w64-mingw32/include",
                "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/opt/include",
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "compilerPath": "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/g++.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-gcc-x64",
            "browse": {
                "path": [
                    "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                    "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                    "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                    "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/include",
                    "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/x86_64-w64-mingw32/include",
                    "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/opt/include",
                    "${workspaceFolder}/**"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ],
    "version": 4
}

今後

Linux, Mac環境でも共通ファイルにできるように調整を続けます。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?