본문 바로가기
etc

[VScode] Visual Studio Code에서 C/C++ 사용하기

by saoh 2023. 4. 4.

1. Extensions에서 C/C++과 C/C++ Extention Pack, Code Runner를 받아준다. 

 

C/C++
C/C++ Extension Pack
Code Runner

 

2. MinGW를 받아준다.

미니멀리스트 버전을 받아주면 된다.

MinGW Minimalist

MinGW - Minimalist GNU for Windows download | SourceForge.net

 

 

MinGW - Minimalist GNU for Windows

Download MinGW - Minimalist GNU for Windows for free. A native Windows port of the GNU Compiler Collection (GCC) This project is in the process of moving to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the G

sourceforge.net

 

설치를 쭉 하다, 표시된 세가지를 선택하고 설치해준다.

 

시스템 환경 변수 편집 - 고급 - 환경변수 - 시스템 변수에서 Path를 클릭한다.

 

시스템 환경 변수 편집 창

 

Path에서 새로 만들기를 클릭하여 

 

C:\MinGW\bin

 

을 입력해준다. MinGW 파일이 있는 곳의 주소를 적어야 하는데, 나는 C 드라이브 바로 아래 두었다.

그리고 확인을 눌러 저장. 

컴퓨터를 리부팅해준다.

 

cmd에 들어가서 

 

gcc -v

g++ -v

 

를 입력해보고, 다음과 같은 결과가 나오면 설정 완료.

 

cmd에서 gcc, g++ 설정 확인

 

3. 테스트 파일을 만들고, Cntl + Shift + P

select a configuration

C/C++: Select a Configuration을 선택한다.

 

 

Edit configurations (UI) 선택. 

UI와 JSON 모두 가능하지만, UI가 더 편리

 

빨간 표시한 부분을 모두 변경

 

 

C++을 사용할 것이기 때문에 g++ 선택. C를 사용할 것이라면 gcc 선택.

 

 

다른 블로그에서는 x64를 선택하였으나, x86을 선택하였음에도 잘 동작.

 

 

본인이 어떤 버전을 사용해야하는지 몰라, 다른 블로그에서 사용한 c17과 c++17로 설정

 

 

4. 위 과정을 마치면 .vscode라는 폴더 안에 세 개의 파일이 생성된 것을 확인 가능.

 

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"
        }
    ]
}

 

5. 키보드 단축키 설정

 

Keyboard Shortcuts로 들어가기

 

 

우측 상단 작은 페이지 모양 클릭.

keybindings.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"
    }
]

 

 

6. 코드 터미널에서 실행.

이대로 실행 파일을 실행하면 터미널에서 실행되지 않아 입력을 받을 수 없다.

 

6-1. Code Runner 설정

 

설정 버튼을 누르고 Extension Settings 클릭

 

Run In Terminal 체크

 

6-2. launch.json 설정 

./vscode 아래 launch.json 파일로 가서 externalConsole true로 변경

 

 

7. 컴파일

만들어두었던 cpp 파일로 가서 Ctrl + Alt + C

 

 

8. 실행 

Ctrl + Alt + R