본문 바로가기

IT 기타/Tool Settings

Microsoft Visual Studio Code - C/C++ 세팅(2023.11. 기준)

1. https://sourceforge.net/projects/mingw-w64/files/mingw-w64/

 

MinGW-w64 - for 32 and 64 bit Windows - Browse /mingw-w64 at SourceForge.net

Mavenlink | Project Management Software Connecting People, Projects, and Profits

sourceforge.net

 

2. C:\mingw64\bin 시스템 환경변수에 등록

 

 

3.  VS Code 열고, 아래 파일 2개 만들기

// test_cpp.cpp

#include <iostream>
using namespace std;
int main()
{
    cout <<"This is cpp"<<endl;
    return 0;
}
//test_c.c

#include <stdio.h>

int main()
{
    printf("This is c");
    return 0;
}

 

 

3. C/C++ Extension Pack 설치

 

 

4-1. F1  -> C/C++: Select a Configuration ... 선택

4-2. Edit Configurations (UI) 선택

4-3. Compiler path 지정 ( c:/mingw64/bin/g++.exe )

4-5. IntelliSense mode 지정 ( windows-gcc-x64 )

 

4-6. Terminal - Configure Tasks...

4-7. Create tasks.json - Template(?) - Others

 

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

 

5-1. File - Preferences - Keyboard Shorcuts

5-2. 우측 상단에 Open Keyboard Shorcuts (JSON) 클릭

 

 

5-3. keybindings.json 아래 내용 붙여넣기

// keybindings.json 내용

[
    { "key" : "ctrl+shift+c", "command" : "workbench.action.tasks.build" },
    { "key" : "ctrl+shift+v", "command" : "workbench.action.tasks.test" }
]

 

 

6. Terminal: Select Default Profile - Command Prompt 선택

 

 

** 세팅 완료

 

 

** 테스트 단계

 

7-1. test_c.c  파일 열기 -> ctrl+shift+c -> save and compile for c -> 컴파일 -> ctrl+shift+v -> 실행 -> Terminal 결과 확인

7-2. test_cpp.cpp 파일 열기 -> ctrl+shift+c -> save and compile for c++ -> 컴파일 -> ctrl+shift+v -> 실행 -> Terminal 결과 확인