VSCode에서 c 언어 디버깅 _Remote-WSL 사용

12980 단어 CWSLVSCode

환경 구축



아래의 링크에 따라 Remote-WSL 환경에서 C 언어 프로그램이 가능할 때까지의 환경 구축을 실시합니다.

Visual Studio Code로 경쟁 프로 환경 구축 (도입편)
VSCode 설치, WSL, Remote-WSL 배포, WSL에 컴파일러 디버깅 설치

VSCode에서의 경쟁 프로용 C++ 환경을 WSL에 Remote Development하는 형태로 만든다
C++ 확장 기능을 WSL에 설치

C 언어용 디버그 설정



launch.json과 tasks.json 만들기



폴더, 소스를 준비합니다.
.vscode가 작성되지 않은 상태입니다.
   
F5를 누른 후 C++(GDB/LLDB)를 클릭합니다.
   
gcc-~를 클릭합니다.
   
.vscode가 생성되고 그 안에 launch.json과 task.json의 두 파일이 생성됩니다.
   

launch.json 및 tasks.json의 내용 (실행 형식 파일 이름 : 소스 이름 확장자를 제외한 이름)



소스 이름이 main.c이면 실행 형식 파일의 이름은 main입니다.
launch.json 및 tasks.json은 자동으로 생성된 파일에서 변경할 필요가 없습니다.

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": [
        {
            "name": "gcc - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "console": "externalTerminal",
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

tasks.json
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc build active file",
            "command": "/usr/bin/gcc",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

launch.json과 tasks.json의 내용 (실행 형식 파일 이름 : a.out)



launch.json 수정 후 (program 만 수정)
{
    // 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 - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "console": "externalTerminal",
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

최적화를 끄고 컴파일이 빨라지도록 -O0 옵션을 붙이고 있습니다.

tasks.json 수정 후 (args 만 수정)
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc build active file",
            "command": "/usr/bin/gcc",
            "args": [
                "-g",
                "-O0",
                "${file}",
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

디버깅



브레이크 포인트를 설정하고 F5, F11, F10을 누르면 디버깅이 가능합니다.
다음은 디버깅 중에 발생한 문제에 대해 설명합니다.

scanf 행에서 디버그 중지하고 있을 때 스텝 인을 하면 에러가 발생



아래 상태에서 스텝 인 (F11 누르기)
   
아래에 오류가 발생합니다.
   
스텝 인이 아니라 스텝 오버 (F10 누름)하면 에러가 발생하지 않습니다.

좋은 웹페이지 즐겨찾기