Developing

VScode 환경에서 C/C++ 개발환경 구축하기 본문

Tips(Reference)/etc

VScode 환경에서 C/C++ 개발환경 구축하기

DEV_BLOG 2021. 4. 6. 13:39

관련건에 대해서 가장 잘 설명을 잘해주신 영상의 링크는 다음과 같다. 

본 게시물은 해당 영상의 방법으로 진행한다.

www.youtube.com/watch?v=3-PD_AUSOLM&ab_channel=%EB%9D%BC%EC%A6%88%EC%9D%B4%EB%85%B8IOT

 

 

1. VSCode를 설치한다.

 

2.VSCode의 왼쪽 메뉴바에서 맨밑의 Extensions 메뉴를 선택한 후, c를 검색한다.

 

 

3.Extension을 Install 해준다.

 

4.아래 사이트에 접속해 MinGW를 Download 후 Install해준다.

 

sourceforge.net/projects/mingw/files/

 

여기서의 Directory위치는 환경변수 PATH를 변경해줄때에 이용된다.

4가지 항목에 대해서 오른쪽마우스 클릭->Mark for Installaion  -> 위의 메뉴바에서 Installation -> APPLY Chagnes

 

 

 

5.환경변수 설정

 

 

sysdm.cpl ,3

윈도우 창에 sysdm.cpl ,3 을 입력한다. ('시스템 환경 변수 편집' 이라고 검색하여도 무방함)

 

 

환경변수 탭을 누른다.

 

PATH변수를 찾아 더블클릭하거나, 편집을 누른다.

새로 만들기 탭을 누르고  C:\MinGW\bin  를 입력해준다.

경우에 따라 환경변수가 바로 반영이 안될 수도있기에, 재부팅을 해야할 수도있다.

(필자의 경우가 이러하였음)

cmd창에서 각각 gcc -v  , g++ -v 을 입력해 적용이 잘 되었는지를 확인할 수 있다.

gcc -v
g++ -v

 

6. VSCODE창을 재실행하고 C 프로젝트 환경을 구축할 WorkSpace를 만든다.

 

7.HelloWorld.c 와같은 프로그램을 작성해준다.

 

8.vscode의 메뉴바에서 Terminal > Configure Default Build Task

 

10. save and compile for c 를 누른다.

 

 

11. 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
                }
            }
        },
        // // 바이너리 실행(Windows)
        {
            "label": "execute",
            "command": "cmd",
            "group": "test",
            "args": [
                "/C",
                "${fileDirname}\\${fileBasenameNoExtension}"
            ]
        }
    ]
}

 

 

12. File > Preferences > Keyboard ShortCuts 를 선택한다.

 

 

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

이렇게 되면 Ctrl+alt+C 로 컴파일, Ctrl+alt+R 로 실행을 할 수 있게된다.

 

 

Ctrl+alr+C

 

Ctrl+alt+R