기타/etc

VScode C++ 환경 구성하기

파송송 2024. 6. 4. 20:14
728x90

vscode에서 C++을 사용하기 위해서는 별도의 조치가 필요하다.

1. MSYS2 설치

https://www.msys2.org/

 

MSYS2

Software Distribution and Building Platform for Windows

www.msys2.org

설치 후 MSYS2 MSYS를 실행한다.


2. 명령어 

pacman -Syn

y를 누르다 보면 터미널이 종료되고  다시 MSYS2를 실행한다.

 

pacman -Su

pacman -S --needed base-devel mingw-w64-x86_64-toolchain

 

mingw64안에 여러 컴파일러들이 들어있는데 나는 그냥 엔터를 눌러 다 설치하였다.


3. 환경 변수 설정

 

Path > 새로 만들기로 들어가 아래의 주소를 추가한다.(주소를 변경했다면 변경한 주소를 넣어줘야 함)

C:\msys64\mingw64\bin

켜져 있는 모든 환경변수 창을 확인을 눌러서 끔


4. VSCode

C를 치면 나오는 Microsoft 3개를 install  해준다.


아래의 설명을 따라하면 다음과 같은 파일이 생김

F1 > C/C++: Edit Configurations(JSON)

c_cpp_properties.json < 아래와 같이 수정

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                // msys64 설치 경로에 따라 수정할 것
                "C:/msys64/mingw64/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.18362.0",
            // msys64 설치 경로에 따라 수정할 것
            "compilerPath": "C:/msys64/mingw64/bin/gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            // windows-msvc-x64 에서 아래와 같이 변경
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}

F1 > Tasks: configure Task 

>Create tasks.json file from template 

>Others

tasks.json

{
  "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",
      "problemMatcher": {
        "fileLocation": [
          "relative",
          "${workspaceRoot}"
        ],
        "pattern": {
          "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning error):\\s+(.*)$",
          "file": 1,
          "line": 2,
          "column": 3,
          "severity": 4,
          "message": 5
        }
      }
    },
    // c comile
    {
      "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 build active file",
      "command": "C:\\msys64\\mingw64\\bin\\gcc.exe",
      "args": [
        "-fdiagnostics-color=always",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ],
      "options": {
        "cwd": "C:\\msys64\\mingw64\\bin"
      },
      "problemMatcher": [
        "$gcc"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "compiler: C:\\msys64\\mingw64\\bin\\gcc.exe"
    }
  ]
}

단축키

Ctrl + K + S

빌드 > CTRL+ALT+C

실행 > CTRL+ALT+R

 

 

CTRL + ALT + C를 하면. exe 파일이 생기고 

CTRL + ALT + R을 하면 코드가 실행된다. 

#include <stdio.h>

int main(void)
{
    printf("Hello World!");
    
    return 0;
}

728x90