Visual Studio Code 에서 C/C++ 개발환경 구축하기
1. C/C++ 컴파일러 설치
1)
링크에 접속하여 Sourcforge를 클릭하면 다운로드 페이지로 이동한다.
5초 후 자동으로 mingw-w64-install.exe가 다운로드 된다.
http://mingw-w64.org/doku.php/download/mingw-builds
Mingw-builds [mingw-w64]
mingw-w64.org
Architecture는 64비트를 지원하기 위해 x86_64를 선택한다. Threads는 호환성이 더 높은 posix로 선택한다.
2)
환경 변수를 등록해야 어떤 경로에서든 gcc를 사용할 수 있다.
윈도우키+R -> 실행창에 sysdm.cpl 입력 -> 고급 -> 환경변수 -> path 선택 편집을 클릭한다.
C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin 경로를 등록한다.
3)
윈도우키+R -> 실행창에 cmd 입력한다.
gcc -v 명령이 실행가능한 지 확인한다.
2. C/C++ 언어지원 설정
1)
액티비티 바에서 확장 아이콘을 클릭하여 C/C++을 입력 한 후, Install을 클릭한다.
(저는 이미 설치하여 Install버튼이 없습니다.)
확장을 적용시키기 위해 Visual Studio Code를 종료시킨 후 재실행합니다.
2)
hello.c 파일을 생성합니다.
간단하게 hello를 출력하도록 만들었습니다.
#include<stdio.h>
int main(){
printf("hello\n");
}
(+hello.cpp 파일을 생성할 경우)
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!" << endl;
}
3)
Ctrl+Shift+P -> 입력 창에 C/C++을 입력한다.
C/C++:Edit Configurations (UI)를 선택합니다.
컴파일러 경로를 g++.exe로 선택한다.
공백이 포함된 경로라서 정상적으로 인식시키기 위해 큰 따옴표를 추가한다.
IntelliSense 모드를 gcc-x64로 선택한다.
C/C++ Configurations 탭을 닫으면 자동으로 저장된다.
.vscode 폴더에 c_cpp_properties.json 파일이 추가되어있다.
3. 단축기 설정
File -> Preferences ->Keyboard Shortcuts 를 클릭한다.
왼쪽 위 표시한 아이콘을 클릭한다.
아래 코드를 입력한 후, 저장(Ctrl+S)한다.
[
{ "key": "ctrl+shift+b", "command": "workbench.action.tasks.build" },
{ "key": "ctrl+shift+t", "command": "workbench.action.tasks.test" }
]
4. 코드 컴파일 및 실행
1)
Terminal -> Configure Default Build Task 를 클릭한다.
Create tasks.json file from template 를 클릭한다.
Othres를 선택한다.
.vscode 폴더에 tasks.json 파일이 추가되어있다.
tasks.json 파일을 아래 코드로 수정한다.
{
"version": "2.0.0",
"runner": "terminal",
"type": "shell",
"echoCommand": true,
"presentation" : { "reveal": "always" },
"tasks": [
//C++ 컴파일
{
"label": "save and compile for C++",
"command": "g++",
"args": [
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"group": "build",
//컴파일시 에러를 편집기에 반영
//참고: https://code.visualstudio.com/docs/editor/tasks#_defining-a-problem-matcher
"problemMatcher": {
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
// The regular expression.
//Example to match: helloWorld.c:5:3: warning: implicit declaration of function 'prinft'
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
},
//C 컴파일
{
"label": "save and compile for C",
"command": "gcc",
"args": [
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"group": "build",
//컴파일시 에러를 편집기에 반영
//참고: https://code.visualstudio.com/docs/editor/tasks#_defining-a-problem-matcher
"problemMatcher": {
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
// The regular expression.
//Example to match: helloWorld.c:5:3: warning: implicit declaration of function 'prinft'
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
},
// 바이너리 실행(Ubuntu)
// {
// "label": "execute",
// "command": "${fileDirname}/${fileBasenameNoExtension}",
// "group": "test"
// }
// 바이너리 실행(Windows)
{
"label": "execute",
"command": "cmd",
"group": "test",
"args": [
"/C", "${fileDirname}\\${fileBasenameNoExtension}"
]
}
]
}
2)
ctrl+shift+b -> C 선택한다.
(+ cpp일 경우 C++ 선택)
3)
ctrl+shift+b -> execute 선택한다.
4)
hello가 출력된다.