크로스 플랫폼 환경에서 VSC용 C 언어 컴파일·디버그 환경 설정하기

바늘테·2025년 8월 27일
0

이 글을 시작하기에 앞서, 세 플랫폼 다 Visual Studio Code로 IDE를 통일해서 씀을 알려드립니다.

  1. 윈도우는 MinGW-w64를 설치해 줍니다. 자세한 설명은 아래 블로그 글을 참조하세요.
    https://dntmdgns03.tistory.com/113
  2. 맥은 CodeLLDB라는 확장 기능을 추가로 깔아 줍니다.
  3. VS Code에서 .vscode라는 폴더 안에, 컴파일 작업용 tasks.json과, 디버그 작업용 launch.json 파일에서 아래 코드로 복붙해 줍시다. 저는 이 코드를 ChatGPT랑 입씨름하며 만들었습니다.
  4. 이러고도 맥에서는 터미널에서 실행이 안 되는 문제가 발생할 수 있는데, 이 경우에는 VS Code의 디버그 탭에서 “Debug C (macOS, CodeLLDB)”로 맞춰져 있는지 확인하실 필요가 있습니다.

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build C program",
            "type": "shell",
            "command": "gcc",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/a.out"
            ],
            "group": "build",
            "problemMatcher": [
                "$gcc"
            ],
            "windows": {
                "command": "gcc",
                "args": [
                    "-g",
                    "${file}",
                    "-o",
                    "${fileDirname}\\a.exe"
                ]
            },
            "linux": {
                "command": "gcc",
                "args": [
                    "-g",
                    "${file}",
                    "-o",
                    "${fileDirname}/a.out"
                ]
            },
            "osx": {
                "command": "gcc",
                "args": [
                    "-g",
                    "${file}",
                    "-o",
                    "${fileDirname}/a.out"
                ]
            }
        },
        {
            "type": "cppbuild",
            "label": "C/C++: clang 활성 파일 빌드",
            "command": "/usr/bin/clang",
            "args": [
                "-fcolor-diagnostics",
                "-fansi-escape-codes",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "디버거에서 생성된 작업입니다."
        }
    ]
}

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug C (Windows)",
      "type": "cppvsdbg",
      "request": "launch",
      "program": "${fileDirname}\\a.exe",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${fileDirname}",
      "console": "integratedTerminal",
      "preLaunchTask": "build C program"
    },
    {
      "name": "Debug C (Linux)",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/a.out",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${fileDirname}",
      "MIMode": "gdb",
      "miDebuggerPath": "/usr/bin/gdb",
      "externalConsole": false,
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "build C program"
    },
    {
      "name": "Debug C (macOS, CodeLLDB)",
      "type": "lldb",
      "request": "launch",
      "program": "${fileDirname}/a.out",
      "args": [],
      "cwd": "${fileDirname}",
      "preLaunchTask": "build C program",
      "stopAtEntry": false,
      "console": "integratedTerminal"
    }
  ]
}
profile
이 나이 처먹고도 여전히 방황 중

0개의 댓글