VSCode와 Ubuntu GDB연결하기(C Debug) + Make 빌드 Debug

hooooon·2022년 10월 22일
11

vscode-C-Debug

목록 보기
3/3

.c 파일 하나에 main이 존재하는 경우 gcc로 디버그가 가능하지만,
링커로 참조해야하는 경우 make로 빌드 및 디버그를 진행합니다.

gdb12.1 을 사용 중이라고 가정하고 공유하겠습니다.
관련 링크는 https://velog.io/@hoon25/VSCode%EC%99%80-Ubuntu-GDB%EC%97%B0%EA%B2%B0%ED%95%98%EA%B8%B0C-Debug
을 참고하시면 됩니다.

1. vscode 설정파일 수정

launch.json, tasks.json에 make 관련 설정을 추가시켜 줍니다.

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.exe - 활성 파일 빌드 및 디버그",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry":false,
            "cwd": "${fileDirname}",
            "environment":[],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/local/bin/gdb",
            "setupCommands": [
                {
                    "description": "gdb에 자동 서식 지정 사용",
                    "text": "-enable-pretty-printing",
                    "ignorefailures": true
                },
                {
                    "description":  "디스어셈블리 버전을 att(으)로 설정",
                    "text": "-gdb-set disassembly-flavor att",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "gcc build"
        },
        {
            "name": "make - 활성 파일 빌드 및 디버그",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry":false,
            "cwd": "${fileDirname}",
            "environment":[],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/local/bin/gdb",
            "setupCommands": [
                {
                    "description": "gdb에 자동 서식 지정 사용",
                    "text": "-enable-pretty-printing",
                    "ignorefailures": true
                },
                {
                    "description":  "디스어셈블리 버전을 att(으)로 설정",
                    "text": "-gdb-set disassembly-flavor att",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "make build"
        }
    ]
}

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "gcc build",
            "type": "shell",
            "command": "gcc",
            "args": [
                "-g",
                "${fileDirname}/${fileBasenameNoExtension}.c",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ],
            "problemMatcher": ["$gcc"],
            "group": "build"
        },
        
        {
            "label": "make build",
            "type": "shell",
            "command": "/usr/bin/make",
            "args": [
                "-C",
                "${fileDirname}",
                "test"
            ],
            "problemMatcher": ["$gcc"],
            "group": "build"
        },

        // 실행
        {
            "label": "execute",
            "command": "cmd",
            "group": "test",
            "args": [
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ]
        }
    ]
}

2. 디버그

선행조건)
1. make 파일이 존재해야합니다.
2. 빌드에 문제가 없어야 합니다.

2-1 C/C++파일 디버그 클릭

2-2 make 설정 클릭

새로 설정한 make기준으로 디버깅을 진행하게 클릭해 줍니다.

2-3 디버그

문제가 없으면 아래와 같이 참조 파일을 디버깅할 수 있습니다.

profile
더 나은 내일을 위해

4개의 댓글

comment-user-thumbnail
2022년 10월 22일

킹...갓..

답글 달기
comment-user-thumbnail
2022년 10월 22일

wowoowowowowowow

답글 달기
comment-user-thumbnail
2022년 10월 23일

awesome

답글 달기
comment-user-thumbnail
2022년 10월 24일

godgodgod

답글 달기