Home New Help Edit

VSCodeでCコンパイルとデバッグ

Suns & Moon Laboratory



はじめに


VSCodeでコンパイル、printf出力とデバッグまで。
複数ソースはちょっと面倒だけど、簡単なテスト程度ならこれでもよさそう。


setup2

2023-10-04
こちらのサイトを参考に設定
Visual Studio CodeでC言語プログラミングを始める(Windows編)

ソース書いてF5押すと、「デバッガーの選択」が表示されるので、「C++(GDB/LLDB)」を選択

setup1

2019-09-09

vscode入れる


VSCode 1.38.0

Extension入れる。
View->Extensions
"C/C++ for Visual Sttudio Code"

MinGW入れる


mingw-geversion 0.63

mingw32-base
mingw32-gcc-g++


PATHに"C:\MinGW\bin"追加

https://qiita.com/N_Matsukiyo/items/464594d1fd3e6ef576a4

ワークスペース


VSCodeはワークスペースが、プロジェクトファイル相当になっています。


順番
File->Add Folder To Workspace...

File->Save Workspace As...

ソース追加(new)

ソース保存(保存しないと、ハイライトとか表示されない)

コンパイル


ソース書いて

ソースウィンドウ選択した状態で、Ctrl+Shift+B押す
押すと上になんか出るのでそれをクリック

C/C++: gcc.exe build active file test_vs_hello2

一回選択すると出なくなる?

コンパイルの設定


複数ファイルの時は、${file}の後ろにファイル名挿入していく。
ビルドは、ビルドしたいソースを選択してCtrl+Shift+Bを押す。

Ctrl+Shift+P押す

"task"入力

"Tasks: Configure Default Build Task"クリック

tasks.json編集

{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "type": "shell", "label": "gcc.exe build active file", "command": "C:\\MinGW\\bin\\gcc.exe", "args": [ "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "C:\\MinGW\\bin" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true } } ] }

デバッグ

Debug->Start Debugging

"C++ (GDB/LLDB)"をクリック

"C/C++: gcc.exe build active file test_vs_hello2"をクリック
2回目は出ない?

DEBUG CONSOLEに出力表示
Loaded xxxxとかいっぱい表示される。
printfした内容もそこに表示される。

F9でブレークポイントのトグル
F5でRUN

launch.json
InternalConsoleOptionを追加すると、F5実行時毎回DEBUG CONSOLEが開かれる。https://code.visualstudio.com/updates/May_2016
{ // 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": [ { "name": "gcc.exe build and debug active file", "type": "cppdbg", "request": "launch", "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "gcc.exe build active file", "internalConsoleOptions": "openOnSessionStart" } ] }

Makefileを使う(Makefile Tools)

2022-05-11
Alma Linux

「VS Code」の「Makefile Tools」拡張機能プレビュー版が公開

ビルドは出来たけど、デバッグ(ブレーク)が出来ない。




参考


君はVS Codeのデバッグの知られざる機能について知っているか


end.

Home New Help Edit
2023-10-04 09:43:52 32400