[VSCode] macOS 기반 Visual Studio Code에 C/C++ 세팅하기(+ 20241025 수정)
macOS 기반 VSCode에서 C/C++ 환경을 세팅하는 법에 대해 서술하겠습니다.
아시다시피, mac에서는 Visual Studio로 바로 C++ 프로그램을 실행시키기 어려운 만큼, VSCode를 활용하게 됩니다.
VSCode는 에디터에 불과한 만큼 추가적인 확장 프로그램 설치 및 실행과 디버깅을 위한 세팅이 필요합니다.
이를 위해 본 글에서는 설치 및 세팅 방법을 서술하고자 합니다.
1. 확장 프로그램 설치
VSCode를 실행시키고, 옆의 확장(Extension) 바를 클릭한 뒤 C/C++, Code Runner, C/C++ Runner를 설치합니다.
이후 Code Runner의 설정(톱니바퀴 모양 버튼)을 누르고 확장 설정(Extension setting)을 누른 뒤, run in terminal을 검색해줍니다. 거기에서 체크 표시를 해 주고, executor map을 검색해서 'settings.json에서 편집'을 누른 뒤 json 파일을 엽니다.
2. JSON 파일 수정
파일 내부에 다음과 같이 입력해주고 저장합니다. (저의 경우 C++17을 버전으로 하였고, 필요에 따라 cpp 부분만 붙여넣으셔도 됩니다.)
{
"workbench.colorTheme": "Default Dark+",
"debug.onTaskErrors": "debugAnyway",
"python.defaultInterpreterPath": "/Users/parkchan-yong/miniconda3/bin/python",
"python.pythonPath": "python3",
"code-runner.executorMap": {
"python": "$pythonPath $fullFileName",
"javascript": "node",
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"zig": "zig run",
"cpp": "cd $dir && g++ -std=c++17 *.cpp -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
},
"editor.minimap.size": "fit",
"explorer.confirmDelete": false,
"[python]": {
"editor.formatOnType": true,
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.codeActionsOnSave": {
"source.organizeImports": false
}
},
"editor.formatOnSave": true,
"window.zoomLevel": -1,
"editor.codeActionWidget.showHeaders": false,
"terminal.integrated.inheritEnv": false,
"lldb.suppressUpdateNotifications": true,
"code-runner.runInTerminal": true,
"cmake.configureOnOpen": true,
"terminal.integrated.shell.osx": "/bin/zsh",
"workbench.editorAssociations": {
"*.ipynb": "jupyter-notebook"
},
"breadcrumbs.enabled": false,
"editor.minimap.enabled": false
}
그 다음 C++ 프로젝트 파일을 열고 파일을 실행시킵니다. 이때 터미널>기본 빌드 작업 구성...순으로 눌러주시고 'clang++ 활성 파일 빌드'를 클릭해주시면 tasks.json 파일이 열리게 됩니다. 이 파일은 아래와 같이 입력해주시면 됩니다.
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
//"${file}",
"${fileDirname}/**.cpp",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
]
}
Code Runner 기반이므로 위 코드가 실제로는 터미널에 아래 코드를 입력하는 것과 같다고 생각하시면 됩니다.
cd $dir && g++ -std=c++17 *.cpp -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
이 다음 디버깅을 시도했을때 저의 경우 에러가 발생하며 launch.json 파일이 열렸고, 여기에서 C/C++ Runner를 설치하여 실행과 디버깅을 할 수 있게 만들었습니다.
수정) Code Runner 기반으로 수정했습니다.
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"preLaunchTask": "g++ build active file",
"stdio": [
null,
null,
null
],
"terminal": "integrated"
},
]
}
+ 20240520
tasks.json에서 "${file}"일 때만 작동하거나 "${fileDirname}/**.cpp"일 때만 작동하는 현상이 발생했습니다.
터미널을 보았을 때, 분할 컴파일을 할 때는 "${fileDirname}/**.cpp"을 사용해야 하고,
그 외의 경우 "${file}"을 사용해야 하는 것으로 추정됩니다.
+ 20241025
실행만을 시키고자 한다면 preLaunchTask를 주석처리시키면 됩니다.
이러면 정확히 아래와 같이 실행됩니다.
cd $dir && g++ -std=c++17 *.cpp -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
// "preLaunchTask": "g++ build active file",
"stdio": [
null,
null,
null
],
"terminal": "integrated"
},
정리 :
특정 파일을 컴파일 및 디버깅할 경우 task.json에서 "${file}"을 사용해야 합니다.
폴더 내 파일들을 컴파일만 시키고자 할 경우 launch.json에서 preLaunchTask를 주석 처리하고 command + option + N을 눌러서 실행하면 되는 것 같습니다.
폴더 내 전체 파일들을 디버깅하고자 한다면 task.json에 사용되는 모든 파일들을 적거나, CMake를 사용해야 할 것 같습니다..
References :
https://areumdawoon.tistory.com/20
<VSCode 를 C++ IDE로 쓰기> Mac에서 C++ 개발하기
일반적으로 C++ 개발한다고하면 대표적으로 쓰는 IDE가 Visual Studio이지만, 맥에서는 Visual Studio가 C++ 개발환경을 지원해주지 않는다. 그래서 대체제로 많이 쓰는게 VSCode다. ▼▼▼ VS Code 설치는 요
areumdawoon.tistory.com
https://junekkk.tistory.com/21
[VSCode] macOS에서 Visual Studio Code로 C/C++ 코딩하기(2) - 디버깅을 위한 tasks.json, launch.json 설정
지난글에서는 VSCode에서 C++ 코드를 '실행(Run)'만 할 수 있는 설정을 했다. [VSCode] macOS에서 Visual Studio Code로 C/C++ 코딩하기(1) - Extension 설치 MacOS에서 Windows의 Visual Studio같은 IDE는 대표적으로 XCode가
junekkk.tistory.com
https://justdoitproject.tistory.com/31
[VSCode] Macbook에서 C/C++ 개발환경 구축하기
Mac OS에서 C/C++ 개발하기 윈도우 운영체제를 쓰는 분들이라면 C/C++ 개발시 Visual Studio를 많이 사용하실텐데요. 저도 Windows를 주로 쓰다가 최근에 맥북프로를 사용하게 되면서 C/C++ 개발환경을 새롭
justdoitproject.tistory.com