Notice
Recent Posts
Recent Comments
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Archives
Today
Total
관리 메뉴

SYDev

vscode에서 C 분할 파일 빌드 및 실행 본문

개발환경/vscode

vscode에서 C 분할 파일 빌드 및 실행

시데브 2023. 8. 8. 19:40

https://cmaven.github.io/vscode/VS-Code-Multi-Compile/

 

Visual Studio Code(vs code) 파일 분할 컴파일

Visual Studio Code 에서 동일 디렉토리 내의 분할된 파일 컴파일에 대해 정리한다.

cmaven.github.io

 

 위 게시물을 평소에 C++ 분할 파일 컴파일할 때 이용했는데, C언어 분할 파일의 경우가 없어서 임의로 추가해봤다.

  

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "runner": "terminal",
    "type": "shell",
    "echoCommand": true,
    "presentation" : { "reveal": "always" },
    "tasks": [
          //C++ 컴파일 - groups
          {
            "label": "save_and_compile_for_C++-groups",
            "command": "g++",
            "args": [                              
                "${fileDirname}/*${fileExtname}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "group": "build",

            //컴파일시 에러를 편집기에 반영
            //참고:   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++ 컴파일 - single
          {
            "label": "save_and_compile_for_C++-single",
            "command": "g++",
            "args": [                              
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "group": "build",

            //컴파일시 에러를 편집기에 반영
            //참고:   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 컴파일 - groups
          {
            "label": "save_and_compile_for_C-groups",
            "command": "gcc",
            "args": [                              
                "-g",
                "${fileDirname}\\*.c",  //모든 C 소스파일을 컴파일
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}" //현재 열려있는 파일과 같은 이름으로 프로그램 생성
            ],
            "group": "build",

            //컴파일시 에러를 편집기에 반영
            //참고:   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 컴파일
        {
            "label": "save_and_compile_for_C",
            "command": "gcc",
            "args": [
                "${file}",
                "-g -o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "group": "build",

            //컴파일시 에러를 편집기에 반영
            //참고:   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
                }
            }
        },
        // 바이너리 실행
        {
            "label": "execute",
            "command": "cmd",
            "group": "test",
            "args": [
               "/C", "${fileDirname}\\${fileBasenameNoExtension}"
            ]
        }
    ]
}

 

-> C++-groups의 경우를 그대로 가져와서 label(사실 이건 안 바꿔도 무방), command(컴파일러를 gcc로), args("${fileDirname}\\*.c"로 설정해놓으면 모든 C 소스파일을 컴파일, 추가로 파일 이름과 프로그램 이름이 같아야 실행됨) 요소만 변경

 

 


참고자료

 

Visual Studio Code(vs code) 파일 분할 컴파일

Visual Studio Code 에서 동일 디렉토리 내의 분할된 파일 컴파일에 대해 정리한다.

cmaven.github.io

 

Visual Studio Code를 사용한 C/C++ 개발환경 만들기(Windows/Ubuntu) - 멈춤보단 천천히라도

Windows와 Ubuntu 환경에 설치된 Visual Studio Code에서 C/C++을 컴파일하고 실행시키는 방법에 대해 설명합니다. 테스트에 사용한 운영체제 버전은 Windows 11과 Ubuntu 22.04입니다. Visual Studio Code 버전에 따라

webnautes.blog

 

[Visual Studio Code]5. C언어 파일 분할 컴파일(GCC)

수성비전자방입니다. 오늘은 Visual Studio Code 시리즈 중 하나로, C언어 파일 분할 컴파일을 하는 방법을 알아보겠습니다. 목차 1. 설치 2. C언어 컴파일 3. C언어 디버깅(gcc, gdb 디버거) 4. C언어 빌드

toopyo.tistory.com