VSCODE 컴파일 문제

ROK·2022년 9월 28일
0

C 관련해서 공부를 계속하면서 자료구조 강의를 들어간 이후 몇 가지 에로사항을 발견했다

vscode에서 gcc, g++을 통해 컴파일 할 때 tasks.json 파일로 컴파일 환경을 설정한다.
지금까지는 단순히 파일 하나만 돌릴 때는 문제가 없었다

하지만 헤더파일, 소스파일, 메인파일을 컴파일 할 때 문제가 생겼다.

파일 하나만 컴파일 할 때는 문제가 없지만 위에서 설명한 파일을 한 번에 돌릴때는 문제가 생기는데 이는 몇 가지 방법을 통해 해결할 수 있다.

우선 tasks.json 파일에서 차이점을 먼저 확인해보자

원래 내가 기존에 사용하던 tasks.json 파일이다

기존 tasks.json 파일

{
  "version": "2.0.0",
  "runner": "terminal",
  "type": "shell",
  "echoCommand": true,
  "presentation": {
    "reveal": "always"
  },
  "tasks": [
    {
      "type": "shell",
      "label": "g++ build",
      "command": "g++",
      "args": [
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ],
      "group": "build",
      "problemMatcher": {
        "fileLocation": ["relative", "${workspaceRoot}"],
        "pattern": {
          "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning error):\\s+(.*)$",
          "file": 1,
          "line": 2,
          "column": 3,
          "severity": 4,
          "message": 5
        }
      }
    },
    {
      "type": "shell",
      "label": "gcc build",
      "command": "gcc",
      "args": [
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ],
      "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++: g++.exe 활성 파일 빌드",
      "command": "C:/mingw64/bin/g++.exe",
      "args": [
        "-fdiagnostics-color=always",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ],
      "options": {
        "cwd": "C:/mingw64/bin"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "디버거에서 생성된 작업입니다."
    }
  ]
}

각각 gcc, g++로 나눠져 있으며 파일 하나만 돌리기에 최적화되어 있다.

하지만 이제 파일을 여러개 돌리기 위해서는 좀 바꿔줘야 하는데
args에서 "${file}""${fileDirname}\\**"으로 바꿔줘야한다.

그 이유는 같은 디렉터리 안에 있는 파일들을 같이 돌리기 위함이고 굳이 번거롭게 따로따로 컴파일 하는 작업을 하지 않아도 된다.

그래서 바뀐 tasks.json 파일은 아래와 같다

{
  "version": "2.0.0",
  "runner": "terminal",
  "type": "shell",
  "echoCommand": true,
  "presentation": {
    "reveal": "always"
  },
  "tasks": [
    {
      "type": "shell",
      "label": "g++ build",
      "command": "g++",
      "args": [
        "${fileDirname}\\**",
        "-g",
        "-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
        }
      }
    },
    {
      "type": "shell",
      "label": "gcc build",
      "command": "gcc",
      "args": [
        "${fileDirname}\\**",
        "-g",
        "-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++: g++.exe 활성 파일 빌드",
      "command": "C:/mingw64/bin/g++.exe",
      "args": [
        "-fdiagnostics-color=always",
        "-g",
        "${fileDirname}\\**",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ],
      "options": {
        "cwd": "C:/mingw64/bin"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "디버거에서 생성된 작업입니다."
    }
  ]
}

여기서 발생하는 문제가 파일 하나만 컴파일하고 싶어도 폴더안의 모든 파일을 다 같이 컴파일하기 때문에 에러가 발생한다는 점이다.

해결방안

첫 번째: 폴더 나누기

첫 번째는 폴더를 나누는 것이다
필요한 파일끼리 묶어서 할 수 있도록 나누는 방법인데, 단점은 한 개씩 돌리는 파일의 경우에는 폴더를 하나씩 만들어줘야 한다는 점이다

두 번째: 터미널에서 명령어 사용

두 번째는 명령어를 통해 컴파일하는 방법이다
tasks.json 파일은 폴더 하위에 있는 파일을 한번에 컴파일 하는 방식으로 그대로 둔채
단일 컴파일이 필요한 파일은 명령어를 사용하고, 여러 파일을 한번에 컴파일 해야하는 경우는 vscode의 컴파일러를 사용한다.

컴파일 명령어

따로 vscode에서 간편하게 해결할 방법을 찾으면 추가할 예정

profile
하루에 집중하자

0개의 댓글