VS Code 에서 c/c++ 사용하기
windows에서 C/C++ IDE를 설치하지 않았을 때 컴파일러를 직접 다운받아 설치해야 하는데 이 때 여러 선택지가 있다. 그 중에서 TDM-GCC를 설명한다. TDM-GCC는 Windows친화적인 컴파일러라고 홈페이지에 설명되어 있다. 홈페이지에서 다운로드 가능하다.
windows에서 c++ 설치할때TDM-GCC외에 cygwin, msys2, mingw등으로 할수있음. mingw가 제일 작고 그 외엔 obj-c같은게 더 들어있다. mingw설치할 때 Threads를 선택할 수 있는데 posix옵션과 x86_64옵션이 있다. 그 차이는,
posix: enable C++11/C11 multithreading features. Makes libgcc depend on libwinpthreads, so that even if you don’t directly call pthreads API, you’ll be distributing the winpthreads DLL. There’s nothing wrong with distributing one more DLL with your application.
win32: No C++11 multithreading features.
64bit에는 Exception을 seh로 사용한다.
다운로드 및 설치
TDM-GCC와 Visual Studio Code는 각각 공식 홈페이지에서 다운받아 설치한다. 환경변수 같은 것은 생략하고 tasks.json
과 launch.json
을 다음과 같이 수정한다.
tasks.json
{
"version": "2.0.0",
"tasks": [{
"label": "g++",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe"
],
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\TDM-GCC-64\\bin\\gdb64.exe",
"preLaunchTask": "g++",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
그리고 miDebuggerPath
는 본인 경로에 맞게 수정하고 두 파일은 모두 Workplace내의 .vscode
폴더에 존재해야 한다.