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?

VSCode + WSL上のUbuntuを使ってgdbでデバックする

0
Last updated at Posted at 2026-07-21

はじめに

VSCode + WSL上のUbuntuを使ってgdbでデバック環境を構築したときにはまったためメモする。

C/C++開発環境のインストール

g++コンパイラをインストール

  1. VSCodeを起動して、WSLに接続する
  2. bashターミナルを開く
  3. 以下のコマンドを入力して、g++コンパイラをインストールする
sudo apt update
sudo apt install --no-install-recommends make g++ libc6-dev

gdbをインストール

  1. 続いてgdbをインストールする
sudo apt update
sudo apt install --no-install-recommends gdb
echo 'export DEBUGINFOD_URLS=""' >> ~/.bashrc

~/.bashrc へ環境変数の更新を登録しないと、gdb開始時に
インターネット経由でライブラリのダウンロードが始まって
数分待たされてしまった。

Downloading separate debug info for /lib64/ld-linux-x86-64.so.2...

launch.json の新規作成

1.プロジェクトのルートに .vscode ディレクトリを作成する
2./vscodeディレクトリ内に launch.json ファイルを新規作成

launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "gdb - 開いているファイルをデバッグ",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/app_debug",
      "cwd": "${fileDirname}",
      "stopAtEntry": true,
      "externalConsole": false,
      "MIMode": "gdb",
      "miDebuggerPath": "/usr/bin/gdb",
      "setupCommands": [
        {
          "description": "gdb の整形表示を有効化",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "build current file (gdb)"
    }
  ]
}

tasks.json の新規作成

1.プロジェクトのルートに .vscode ディレクトリを作成する
2./vscodeディレクトリ内に tasks.json ファイルを新規作成

tasks.json
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build current file (gdb)",
      "type": "shell",
      "command": "make",
      "args": [
        "-C",
        "${fileDirname}/..",
        "debug"
      ],
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

Makefileの作成

  1. コンポーネントのルートに Makefileを新規作成する
CXX      := g++
INC_PATHS := . ..
INC_FLAGS:= $(addprefix -I,$(INC_PATHS))

CXXFLAGS := -std=c++17 -Wall -Wextra -O2 $(INC_FLAGS)
DBGFLAGS := -std=c++17 -Wall -Wextra -g $(INC_FLAGS)

TARGET_DIR  := tests
TARGET_NAME := app

TARGET      := $(TARGET_DIR)/$(TARGET_NAME)
DEBUG_TARGET:= $(TARGET_DIR)/$(TARGET_NAME)_debug

SRCS     := tests/main.cpp
OBJS     := $(SRCS:.cpp=.o)
INCLUDES := simple_json.hpp

all: $(TARGET)

$(TARGET): $(OBJS)
	$(CXX) $(CXXFLAGS) $(OBJS) -o $(TARGET)
	chmod +x $(TARGET)

%.o: %.cpp $(INCLUDES)
	$(CXX) $(CXXFLAGS) -c $< -o $@

run: $(TARGET)
	./$(TARGET)

# デバッグ用ターゲット
debug: $(SRCS) $(INCLUDES)
	$(CXX) $(DBGFLAGS) $(SRCS) -o $(DEBUG_TARGET)
	chmod +x $(DEBUG_TARGET)

clean:
	rm -f $(OBJS) $(TARGET) $(DEBUG_TARGET)

.PHONY: all run debug clean

デバック手順

  1. VSCodeにて、デバック対象のファイルを開く
  2. ブレークしたい行で [F9]キーを入力する
  3. [F5]キーを入力するとコンパイルが開始され、続いてデバックが開始される

おまけ

# ビルド
make

# デバック用ビルド
make debug

# クリーン
make clean

# 実行
make run

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?