tasks.json 이란?
많은 도구가 코드 린팅, 빌드, 패키징, 테스팅, 또는 소프트웨어 시스템 배포와 같은 작업을 자동화하기 위해 존재합니다.
이러한 도구들은 주로 터미널에서 실행되며 개발자는 터미널 또는 명령 프롬프트에서 명령을 입력하여 이러한 도구를 사용하게 됩니다.
하지만 tasks.json 파일을 사용하게 되면 터미널에서 실행시키거나 새로운 코드를 작성할 필요가 없으며, 개발 프로세스를 더 효율적으로 관리할 수 있게 됩니다.
워크스페이스나 특정 폴더에 대한 작업은 .vscode 폴더 내에 있는 tasks.json 파일에서 구성됩니다.
이 글에서는 VS code에서 c와 c++로 작성한 파일을 컴파일하고 실행할 수 있도록 tasks.json을 구성할 것입니다.
VS code tasks.json 작성
- VS code Extensions에서 Command Variable을 설치합니다.
- ctrl+shift+p를 누른 후 Tasks: Configure Task > Create tasks.json file from template > Others에 들어갑니다.
- tasks.json 파일에 다음과 같이 코드를 작성합니다.
{ "version": "2.0.0", // tasks.json 파일 형식 버전 "runner": "terminal", // 작업을 실행할 때 사용할 실행기 (터미널) "type": "shell", // 터미널의 유형 (shell) "echoCommand": true, // 실행 명령을 터미널에 표시할지 여부 "presentation": { "reveal": "always" // 실행 중 출력 창을 항상 표시할지 여부 }, "tasks": [ // 실행할 작업 목록 시작 { "label": "save and compile for C/C++", // 작업의 이름 "command": "${input:chooseCompiler}", // 컴파일러 종류 "args": [ "${file}", // 현재 파일의 경로 "-Wall", "-Wextra", "-Werror", "-o", "${fileDirname}/${fileBasenameNoExtension}" // 실행파일 이름 ], "presentation": { "reveal": "always", "panel": "new" // 새로운 출력 패널에 결과 표시 }, "group": { "kind": "build", "isDefault": true // 기본 빌드 작업으로 설정 }, "problemMatcher": { "fileLocation": [ "relative", "${workspaceFolder}" ], "pattern": { "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning error):\\s+(.*)$", "file": 1, "line": 2, "column": 3, "severity": 4, "message": 5 } }, "detail": "Compile for C/C++ Made by tkxx_ls" }, { "label": "build for C/C++", "command": "${input:chooseCompiler}", "args": [ "-fdiagnostics-color=always", "-g", "-Wall", "-Wextra", "-Werror", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}.exe" ], "problemMatcher": { "fileLocation": [ "relative", "${workspaceFolder}" ], "pattern": { "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$", "file": 1, "line": 2, "column": 3, "severity": 4, "message": 5 } }, "group": "build", "presentation": { "reveal": "always", "panel": "new", "focus": true, "echo": true }, "detail": "Build for debugger Made by tkxx_ls" }, { "label": "execute", "command": "cmd", "group": "test", "args": [ "/C", "${fileDirname}/${fileBasenameNoExtension}.exe" ], "detail": "Execute task for exe file Made by tkxx_ls", "dependsOn": ["save and compile for C/C++"] } ], "inputs": [ { "id": "chooseCompiler", "type": "command", "command": "extension.commandvariable.file.fileAsKey", "args": { ".cpp": "g++", ".c": "gcc" } } ] }
- 저장한 뒤 cpp파일이나 c파일 하나 생성한 후 샘플 코드를 작성한 뒤 컴파일과 실행되는지 확인합니다.
참고 자료
Get Started with C++ and MinGW-w64 in Visual Studio Code
Configuring the C++ extension in Visual Studio Code to target g++ and gdb on a MinGW-w64 installation
code.visualstudio.com
Command Variable - Visual Studio Marketplace
Extension for Visual Studio Code - Calculate command variables for launch.json and tasks.json
marketplace.visualstudio.com
'환경설정 > c & c++' 카테고리의 다른 글
[VS CODE] C/C++ launch.json 작성 (0) | 2024.05.07 |
---|---|
[VS CODE] C++ Snippet 설정 (0) | 2023.09.14 |