[환경 설정] VS Code 에서 C/C++ 코딩 환경 구축하기 (Windows)

Kim Yuhyeon·2022년 3월 3일
13

C++

목록 보기
1/25

1. VS Code 실행 후 확장 프로그램 설치

2. MinGW 프로그램 설치

프로그램 설치 안내 블로그 << 자세히 나와있음 !

3. 환경 변수 등록

  1. 제어판 > 시스템 환경 변수 편집 > 고급 > 환경변수 > 시스템 변수 > PATH 누르고 편집
  2. 새로만들기 > C:\MinGW\bin 추가

4. 설치가 제대로 되었는지 확인

cmd 열어서 gcc -v , g++ -v 눌러서 버전 체크 해보기

5. VS Code 재실행 후 설정

  1. 터미널 > 기본 빌드 작업 구성(Ctrl + Shift + B) > 빌드 작업 구성

  2. tasks.json 내용 복붙

{
    "version": "2.0.0",
    "runner": "terminal",
    "type": "shell",
    "echoCommand": true,
    "presentation": {
        "reveal": "always"
    },
    "tasks": [
        {
            "label": "save and compile for C++",
            "command": "g++",
            "args": [
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "group": "build",
            "problemMatcher": {
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}"
                ],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        },
        {
            "label": "save and compile for C",
            "command": "gcc",
            "args": [
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "group": "build",
            "problemMatcher": {
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}"
                ],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        },
        {
            "label": "execute",
            "command": "cmd",
            "group": "test",
            "args": [
                "/C",
                "${fileDirname}\\${fileBasenameNoExtension}"
            ]
        },
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe 활성 파일 빌드",
            "command": "C:\\MinGW\\bin\\gcc.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "디버거에서 생성된 작업입니다."
        },
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 활성 파일 빌드",
            "command": "C:\\MinGW\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
//            "group": {
//               "kind": "build",
//                "isDefault": true
//            },
            
            "detail": "디버거에서 생성된 작업입니다."
        },
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe 활성 파일 빌드",
            "command": "C:\\MinGW\\bin\\gcc.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
              
            // "group": {
            //     "kind": "build",
            //     "isDefault": true
            // },

            "detail": "컴파일러: C:\\MinGW\\bin\\gcc.exe"
        }
    ]
}
  1. 단축키 설정

파일 > 기본 설정 > 바로 가기 키

  1. keybingings.json 내용 복붙
// Place your key bindings in this file to override the defaults
// 아래 키 바인딩을 파일에 넣어서 기본값을 덮어 씁니다.


[
    // 컴파일
    {
        "key": "ctrl+alt+c",
        "command": "workbench.action.tasks.build"
    },
    // 실행
    {
        "key": "ctrl+alt+r",
        "command": "workbench.action.tasks.test"
    }
]
  1. 빌드 Ctrl + Alt + C > save and comfilr for C/C++

  2. 실행 Ctrl + Alt + R > execute

6. 완료

💡 참고 포스팅

rasino님 블로그

3개의 댓글

comment-user-thumbnail
2022년 5월 4일

mac도 VScode로 C++하게 해주세요 우에에엥엥

답글 달기
comment-user-thumbnail
2022년 9월 19일

왓! 학교 과제 때문에 VS Code에서 C를 구동해야 했는데, 포스팅 해주셔서 감사합니다!

답글 달기
comment-user-thumbnail
2023년 3월 10일

안녕하세요.
본문대로 세팅하니 컴파일과 실행은 잘되는데 vscode 내부 터미널에 뜨는 출력 중 한글이 전부 깨져버리네요ㅠㅠ
외부 터미널로 띄우거나 하는 방법은 없을까요?

답글 달기