본문 바로가기

Tech/Tips

맥에서 VS code로 C/C++ 빌드, 실행 개발 환경설정

반응형

이미지 출처 : Pixabay

맥에서 사용할 가벼운 C/C++ 에디터를 찾으려니 vs code가 답인것 같아서 혼자 세팅해본 과정을 글로 남기기로 했습니다.

Download VS Code for MAC : https://code.visualstudio.com/download

 

Download Visual Studio Code - Mac, Linux, Windows

Visual Studio Code is free and available on your favorite platform - Linux, macOS, and Windows. Download Visual Studio Code to experience a redefined code editor, optimized for building and debugging modern web and cloud applications.

code.visualstudio.com

다운로드 후 실행하여 Extension 버튼을 클릭합니다.

vs code for mac

그리고 C/C++ Extension을 설치해 줍니다.

vs code for mac

그리고 프로젝트 폴더를 연 다음, helloC.c, helloCPP.cpp 두 파일을 생성해 줍니다.

(주황색 네모 버튼이 new file 버튼입니다.)

vs code for mac - new file

helloC.c

#include <stdio.h>

int main(void) {
    printf("hello world, C!\n");
    return 0;
}

helloCPP.cpp

#include <iostream>

int main(void) {
    std::cout<<"Hello world, CPP!"<<std::endl;
    return 0;
}

그리고 ⇪ + ⌘ + b (shift + command + b) 를 입력합니다.

아래 gcc build active file 에 마우스를 올려 오른쪽 톱니바퀴를 눌러주면 다음 화면을 볼 수 있습니다.

C/C++ gcc build active file

그러면 tasks.json 파일이 생성됩니다.

tasks.json

tasks.json을 다음과 같이 구성합니다.

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "gcc build active file",
            "command": "/usr/bin/gcc",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.out"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        },
        {
            "type": "shell",
            "label": "g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.out"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        },
        {
            "label": "exec",
            "type": "shell",
            "command": "${fileDirname}/${fileBasenameNoExtension}.out",
            "group": "build"
        }
    ]
}

//2020.04.25. 추가 : 다중 파일 빌드 옵션

Undefined symbols for architecture x86_64:

     .....

     .....

ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The terminal process terminated with exit code: 1

Terminal will be reused by tasks, press any key to close it.

제보 감사합니다.

linker 오류가 발생하는 경우가 생겨 이에 대한 해결법을 안내드립니다 : 

main.cc 파일 빌드 시 main.cc 가 include 중인 헤더파일에 정의된 클래스의 메소드를 정의한 .cc 파일이 같이 빌드되지 않아 링크 문제가 발생하는것을 확인하였습니다.

따라서 그러한 문제를 해결하기 위해서는 tasks.json 의 내용을 다음과 같이 살짝 수정해주면 됩니다. : 

(수정된 라인 : "args :  .. "${file} 에서 ${fileDirname}/*.cc 로 수정됨)

        {
            "type": "shell",
            "label": "g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${fileDirname}/*.cc",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.out"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        },
        {
            "label": "exec",
            "type": "shell",
            "command": "${fileDirname}/${fileBasenameNoExtension}.out",
            "group": "build"
        }

args : 항목 내 -g 인자의 옵션을 ${fileDirname}/*.cc (혹은 ${fileDirname}/*.cpp) 로 수정해주시면 정상적으로 빌드됩니다.


그리고 hello.C 파일에서 다시 ⇪ + ⌘ + b (shift + command + b) 를 입력하면 다음 리스트를 볼 수 있습니다.

C 파일을 빌드하려면 gcc를 사용하여야 하므로 gcc build를 선택합니다. (반대로 C++ 빌드시는 g++)

gcc build

그리고 다시 ⇪ + ⌘ + b (shift + command + b) 를 입력하여 exec를 선택해주면 실행 결과를 볼 수 있습니다.

without scanning the task output

gcc로 .c 파일 빌드 후 exec

Hello world, C!

g++로 .cpp 파일 빌드 후 exec

Hello world, C++!

반응형