본문 바로가기
컴퓨터/C & C++

[vscode] m1 mac에서 vscode c++ debugger 사용하기

by HanJoohyun 2023. 6. 24.
반응형
반응형

안녕하세요 한주현입니다

 

오늘은 apple silicon (m1 mac)에서 vscode c++ debugger를 사용하는 방법에 대해서 알아보겠습니다.

 

들어가며

요새 한창 c++로 개발을 많이 하고있는데요,

c++로 개발을 윈도우 PC가 아닌 apple silicon mac으로 하다보니 이래저래 불편한 감이 상당한데요

스타벅스 입장권인 macbook을 쓰는 입장에서 m1 mac에서 꿋꿋이 c++ 개발을 하기 위해 apple silicon mac에서 vscode c++ debugger를 원활히 사용하는 방법에 대해 적어보겠습니다.

 

참고로 이 포스트는 apple silicon CPU에서의 내용을 담고 있습니다. m1 mac에서는 gdb가 지원하지 않기 때문에 CodeLLDB를 설치해서 사용해야해서 이 포스트를 작성하게 되었습니다!

 

설치 필요 항목들

1. vscode

2. vscode extension (C/C++, C/C++ Extension Pack, CodeLLDB)

아래 그림을 참고해서 extension 을 설치해주세요!

설정하기

1. vscode에서 프로젝트 폴더를 엽니다. 여기 아래에서 디버거에 필요한 파일들을 생성하겠습니다.

2. .vscode 디렉터리와 launch.json, tasks.json 을 생성합니다. (디렉터리 이름이 "쩜"vscode 입니다. 쩜을 넣어야 함을 주의해주세요)

 

3. 아래와 같이 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": "Debug",
            "type": "lldb",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.o",
            "args": [
                "Hello",
                "World"
            ],
            "cwd": "${fileDirname}/",
            "preLaunchTask": "C/C++: g++ build active file"
        }
    ]
}

 

tasks.json

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

4. debugger가 잘 작동하는지 테스트 해봅시다. 아래와 같이 debugger_test.cpp 코드를 작성합시다.

#include <iostream>

int main(int argc, char **argv) {
    for (int i = 0; i < argc; i++) {
        std::cout << "argv[" << i << "]: " << argv[i] << std::endl;
    }
    int n = 3;
    for (int i = 0; i < n; i++) {
        std::cout << i << std::endl;
    }
    return 0;
}

저는 8번 라인 옆에 breakpoint 를 설정하였습니다. 옆에 클릭을 하게 되면 빨간색 dot으로 breakpoint 가 생성됩니다.

 

5. debugger 실행. F5 키를 눌러서 디버거가 수행되도록 합니다. 맥북에서는 fn+F5를 눌러야 F5가 수행됩니다.

디버거를 수행하면 아래 스크린샷과 같이 breakpoint에서 멈춰있는 것을 확인하실 수 있습니다!

 

잘 되네요 ㅎㅎ

디버거 창에서 옆의 WATCH에 보고싶은 변수를 넣어서 변수의 값들을 확인하실 수 있습니다.

F5키를 눌러서 다음 breakpoint 로 넘어가거나,

F10키를 눌러서 한 줄씩 실행 해 볼 수도 있습니다.

 

마치며

오늘은 애플 실리콘 (m1 mac)에서 vscode debugger 사용하는 방법에 대해 알아보았습니다.

 

해당 내용들은 아래 github repository에도 남겨두었습니다!

https://github.com/KennethJHan/vscode_cpp_debugger_instruction

 

GitHub - KennethJHan/vscode_cpp_debugger_instruction: vscode_cpp_debugger_instruction

vscode_cpp_debugger_instruction. Contribute to KennethJHan/vscode_cpp_debugger_instruction development by creating an account on GitHub.

github.com

여러분들께 도움이 되셨으면 좋겠습니다!

 

그럼 다음에 또 만나요!

반응형

댓글