본문 바로가기
[ETC] - C++

# 0. VSCode C++ 환경설정

by Bebsae 2021. 6. 14.

https://sourceforge.net/projects/mingw/files/

 

MinGW - Minimalist GNU for Windows - Browse Files at SourceForge.net

×

sourceforge.net

 

위 링크에서 최신 버전의 MinGW.exe을 다운로드한다.  그 다음 다운로드 받은 실행파일을 펀쿨섹좌 빙의하여 continue 하고 다음과 같은 4개의 패키지를 체크한다. 체크하는 방법은 우클릭 후 "Mask for Installation"을 클릭하면 된다.

 

체크

 

체크를 마치면 좌측 상단의 Installation 탭의 Apply Change를 클릭한 후, 새로 뜬 창에서 Apply 버튼을 누르면 된다.

 

변경사항 적용

 

그리고 환경변수를 지정할 차례이다. 

 

시스템 속성

Windows 10 검색에서 "시스템 환경 변수 편집"을 치면 다음과 같은 창이 나올 것이다. 환경 변수(N)를 클릭해주자.

 

환경변수 추가

시스템 변수(S) 에서 Path 변수를 선택한 다음 편집을 클릭한다. 새로 만들기 (N)을 클릭한 다음 찾아보기(B)를 통해 아까 설치한 MinGW의 경로의 bin 폴더를 선택해주고 확인! 확인!

 

 

 

 

 

 

 

 

 

지금부터는 에디터에서 여태까지 설치한 컴파일러를 연동해줄 단계이다. 먼저  VSCode에서 C/C++ Extension을 설치해주어야 한다.

 

Extension 설치

빨간색 아이콘을 클릭하고 C/C++을 검색하여 다음과 같은 모듈을 설치해준다.

 

VSCode에서 Ctrl + Shift + P를 누르면 다양한 메뉴들이 나올 것이다. 그중에서 "C/C++: Edit Configurations (UI)"를 클릭하면 다음과 같은 메뉴가 나온다.

 

컴파일러를 지정해주어야 한다. 아까 환경변수 세팅할때 지정했던 경로에서

C/C++ Extension Configurations

 

g++.exe 파일의 경로를 입력해주자.

 

 

Terminal - Configure Default Build Task 에서 g++.exe 를 선택하자.

그러면 .vscode에 tasks.json이 생성되었을 텐데 해당 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}"
            ]
    
        }
    ]
}

 

그 다음 좌측 상단의 File - Preferences - Keyboard Shortcuts 에서 빨간 아이콘을 클릭하면 keybindings.json 문서가 표시된다.

 

 

keybindings.json 의 내용을 다음과 같이 수정한다.

[
    //컴파일
    { "key": "ctrl+alt+c", "command": "workbench.action.tasks.build" },
    
    //실행
    { "key": "ctrl+alt+r", "command": "workbench.action.tasks.test" }
]

 

EXPORER에서 New File을 클릭한 후, hello.cpp 예시 파일을 생성하고 다음과 같은 코드를 작성하자. 그 다음으로는 빌드 후 실행할 일만 남았다! 

 

#include <iostream>

int main(int argc, char **)
{
    std::cout << "Hello, World!" << std::endl;
    std::cout << "argc: " << argc << std::endl;
}

 

keybindings.json 에서 빌드는 Ctrl + Alt + C, 실행은 Ctrl + Alt+ R 로 단축키를 지정했다.

 

 

우선 빌드를 먼저 해야 한다.

 

Ctrl +&nbsp;Alt + B

 

C++을 빌드할 경우에는 save and compile for C++을 선택하면 된다.

 

 

그 다음 실행을 하면 된다.

Ctrl + Alt + R

 

실행결과

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

[참고]

https://evandde.github.io/vscode-cpp/

 

Visual Studio Code에서 C, C++ 환경 설정하기

VSCode에서 C 언어 및 C++ 언어를 사용하기 위해 실행/디버깅 등이 가능한 환경을 구축하는 방법에 대해 알아봅니다.

evandde.github.io

 

위의 포스트 내용대로 choco 패키지 관리자를 이용하여 mingw를 쉽게 설치할 수 있다. 하지만, 해당 포스트를 작성한 시점 기준으로 gcc.exe는 설치되지만, gdb.exe가 누락되어 다음과 같은 오류가 발생한다.

 

https://github.com/microsoft/vscode-cmake-tools/issues/692

 

Unable to start debugging. The value of miDebuggerPath is invalid problem · Issue #692 · microsoft/vscode-cmake-tools

Hello, i'm new at coding and normaly i code websites and things like this, i get try to code one program is my long therm project in C++ so i try my best, now i just testing how to do data base...

github.com

 

그래서 필자는 choco 패키지 관리자를 사용하지 않고 환경을 구축하기로 했다.

 

https://webnautes.tistory.com/1158

 

Visual Studio Code에서 C/C++ 프로그래밍( Windows / Ubuntu)

Windows와 Ubuntu 환경에 설치된 Visual Studio Code에서 C/C++을 컴파일하고 실행시키는 방법에 대해 설명합니다. 테스트에 사용한 운영체제 버전은 Windows 10과 Ubuntu 20.04입니다. Visual Studio Code 버전에..

webnautes.tistory.com

 

댓글