콘텐츠로 건너뛰기

윈도우

윈도우 VSCODE에서 C언어(C++) 개발환경 구축하기!!

1. 먼저 VS 코드를 설치합니다
https://code.visualstudio.com/

2. VSCODE 설치후 Extensions(Ctrl + Shift + X) 에서 ‘C/C++’ 로 검색 후 설치합니다

3. MinGw를 설치 합니다
https://sourceforge.net/projects/mingw/

설치시 아래 4개의 패캐지를 선택하고 Installation – Apply Changes를 선택하시면 됩니다

mingw-developer-toolkit
mingw32-base
mingw32-gcc-g++
msys-base

4. MinGw 설치 후 환경 변수에 등록해야 되는데 윈도우키 + R키를 누른후 sysdm.cpl를 입력 – 고급 – 환경 변수를 연 다음 C:\MinGW\bin를 등록해 줍니다(혹시 다른 폴더에 설치했다면 설치한 폴더를 지정해 줍니다)

5. VSCODE를 실행하고 C나 C++를 진행할 폴더하나를 만들고 File – Open Folder로 해당 폴더를 엽니다

6. 프로젝트에 파일하나를 추가하고 main.c라고 추가한뒤 아래 내용을 추가합니다(테스트를 위한 내용이라 아무 C코드라도 상관 없습니다)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main()
{
printf("hello world");
return 0;
}
#include <stdio.h> int main() { printf("hello world"); return 0; }
#include <stdio.h>

int main()
{
    printf("hello world");
    return 0;
}

7. VSCODE 에서 F1키를 눌러 창이 뜨면 Create task.json file from template를 입력합니다
혹시 아무것도 나타나지 않으면 Tasks: Configure Task를 입력한 후 Create task.json file from template를 선택해 줍니다
그 후 Other를 선택해 주면 json 파일이 하나 만들어 집니다.
아래 내용을 해당 파일에 복사해 줍니다

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"version": "2.0.0",
"runner": "terminal",
"type": "shell",
"echoCommand": true,
"presentation": {
"reveal": "always"
},
"tasks": [
//C++ compile
{
"label": "save and compile for C++",
"command": "g++",
"args": [
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"group": "build",
//refer: 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 compile
{
"label": "save and compile for C",
"command": "gcc",
"args": [
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"group": "build",
//refer: 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
}
}
},
// binary exec(Windows)
{
"label": "execute",
"command": "cmd",
"group": "test",
"args": [
"/C",
"${fileDirname}\\${fileBasenameNoExtension}"
]
}
]
}
{ "version": "2.0.0", "runner": "terminal", "type": "shell", "echoCommand": true, "presentation": { "reveal": "always" }, "tasks": [ //C++ compile { "label": "save and compile for C++", "command": "g++", "args": [ "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "group": "build", //refer: 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 compile { "label": "save and compile for C", "command": "gcc", "args": [ "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "group": "build", //refer: 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 } } }, // binary exec(Windows) { "label": "execute", "command": "cmd", "group": "test", "args": [ "/C", "${fileDirname}\\${fileBasenameNoExtension}" ] } ] }
{
    "version": "2.0.0",
    "runner": "terminal",
    "type": "shell",
    "echoCommand": true,
    "presentation": {
        "reveal": "always"
    },
    "tasks": [
        //C++ compile
        {
            "label": "save and compile for C++",
            "command": "g++",
            "args": [
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "group": "build",
            //refer:   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 compile
        {
            "label": "save and compile for C",
            "command": "gcc",
            "args": [
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "group": "build",
            //refer:   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
                }
            }
        },
        // binary exec(Windows)
        {
            "label": "execute",
            "command": "cmd",
            "group": "test",
            "args": [
                "/C",
                "${fileDirname}\\${fileBasenameNoExtension}"
            ]
        }
    ]
}

8. VSCODE에서 File – Preferences -Keyboard Shortcuts로 단축키 옵션을 연다음 우측 상단의 Open Keyboard Shotcuts를 열어서 아래 내용을 복사해서 붙여 넣어 줍니다

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[
// 컴파일
{
"key": "ctrl+alt+c",
"command": "workbench.action.tasks.build"
},
// 실행
{
"key": "ctrl+alt+r",
"command": "workbench.action.tasks.test"
}
]
[ // 컴파일 { "key": "ctrl+alt+c", "command": "workbench.action.tasks.build" }, // 실행 { "key": "ctrl+alt+r", "command": "workbench.action.tasks.test" } ]
[
    // 컴파일
    {
        "key": "ctrl+alt+c",
        "command": "workbench.action.tasks.build"
    },
    // 실행
    {
        "key": "ctrl+alt+r",
        "command": "workbench.action.tasks.test"
    }
]

9. 이제 모든 설정이 끝났습니다 이제 제일 중요한 작업이 남았습니다 꼭 VSCODE를 껐다가 켜주세요

10. 다시 실행 후 Ctrl + alt + C 로 컴파일 하면 GCC 선택창이 뜹니다 GCC 선택해 주고

11. Ctrl + alt + R 로 실행하면 터미널에 결과가 나타납니다