지난번 포스팅에서 VS Code 설치 그리고 간략한 C++ 디버깅 방법에 대해 알아보았다. 그런데 디버깅시 파라미터 전달과 같은 디버그 옵션을 설정하기 위해서는 VS Code 에서 launch.json 을 생성해야 한다. DBow3 라이브러리와 OpenCV 를 사용하여 이미지를 검색하는 프로젝트를 간단하게 만들고 CMake 를 사용해 프로젝트를 빌드하고 VS Code 를 통해 디버깅 해보는 것을 알아보자.
먼저 아래 링크에서 DBow3 를 사용하는 예제 프로젝트를 다운로드 하자. 아래 프로젝트는 DBoW3 라이브러리를 사용한 장면 인식 예제이다.
GitHub - bkkim/DBow3_example: DBow3's applied example modified from general_demo
DBow3's applied example modified from general_demo - GitHub - bkkim/DBow3_example: DBow3's applied example modified from general_demo
github.com
먼저 위의 레포지토리에 나와 있는 대로 OpenCV 와 DBoW3 를 설치한다. 그리고 DBow3_example 프로젝트를 다운로드 하고 프로젝트가 정상적으로 동작하는지 Test 해보자.
$ git clone https://github.com/bkkim/DBow3_example.git
$ cd DBow3_example
$ mkdir build && cd build
$ cmake ..
정상적으로 프로그램이 실행한다면 VS Code 를 실행하고 디버깅을 위한 json 파일을 생성하자. 먼저 터미널에서 DBow3_example 폴더로 이동하고 code . 명령을 통해 VS Code 를 실행시킨다. 이제 컴파일에 필요한 json 파일인 tasks.json 파일을 생성하자.
위 그림에서 Terminal 메뉴의 Configure Default Build Task ... 메뉴를 누르면 빌드 Task를 선택할 수 있는데 CMake : Build 를 선택하자. 그럼 아래와 같이 Tasks.json 파일이 생성된다.
{
"version": "2.0.0",
"tasks": [
{
"type": "cmake",
"label": "CMake: build",
"command": "build",
"targets": [
"all"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [],
"detail": "CMake template build task"
}
]
}
이제 Ctrl+Shift+B 버튼을 누르면 빌드를 하게 되고 Build 디렉토리에 실행파일이 생성된다. 이제 디버깅을 위한 json 파일인 launch.json 파일을 만들자.
디버그 탭에서 Run and Debug 버튼 아래의 create a launch.json file. 텍트를 클릭하면 우측에 launch.json 파일이 생성된다. 이때 "configurations" 항목 하단에 "name" : "(gdb ... 까지만 입력하면 아래와 같이 configuration 을 선택할 수 있는 드롭다운 메뉴가 나타나는데 여기서 C/C++: (gdb) Launch 를 선택하자.
그러면 아래와 같이 launch.json 파일이 생성된다. 이때 "program" 항목과 "args" 항목은 실행할 프로그램과 프로그램 파라미터를 넣어주어야 한다. 위 DBow3_example 의 디버깅을 위해서는 아래와 같이 build 디렉토리에 실제 생성된 실행파일과 실행파일 파라미터를 입력해야 한다.
{
// 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/build/DBoW3_exam_main",
"args": ["orb", "/home/kbk/DBow3_example/data/office/db_set", "/home/kbk/DBow3_example/data/office/test_set"],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
이제 디버깅을 시작할 코드에 디버그 포인트를 셋팅하고 디버그를 실행하면 아래와 같이 브레이크 포인트에서 디버깅이 걸리는 것을 확인 할 수 있다.
'IT 스터디 > C, C++' 카테고리의 다른 글
C/C++ 개발할 때 SDL 옵션은 뭘까? (0) | 2023.11.02 |
---|---|
error C2065: 'M_PI': 선언되지 않은 식별자입니다. (feat. openMVG) (1) | 2023.10.19 |
collect2: error: ld returned 1 exit status 에러?? (0) | 2023.08.11 |
CMake 간단 사용법 (1) (0) | 2023.08.01 |
atan2() 함수의 의미와 사용 (0) | 2023.06.22 |