Ubuntu에 C++ 세팅

Ubuntu에 C++ 세팅

Summary Ubuntu에서 C++ 개발 환경을 설정하는 방법: apt 업데이트 후 g++ 또는 build-essential 설치, CMake 설치, VSCode에서 C/C++ 확장 설치 및 설정, tasks.json 파일 생성 및 단축키 설정을 포함합니다.


💡 목차

🔗 https://roytravel.tistory.com/381

🔗 https://wikidocs.net/205073

💡 목차

Terminal에서 C++ 및 CMake 설치

apt 업데이트

1
2
sudo apt update
sudo apt upgrade

두 방법 중 하나 선택하여 설치

1
sudo apt install g++
1
sudo apt install build-essential

대부분의 경우, sudo apt install build-essential 명령을 사용하는 것이 더 편리하며, C++ 개발 환경을 한 번에 설정할 수 있습니다. 이 명령을 사용하면 필수 도구뿐만 아니라 다른 유용한 도구도 함께 설치됩니다.

설치 확인

1
which c++

CMake 설치

1
sudo apt install cmake

VSCode에서 C/C++ extension 설치

Image

  1. Ctrl + Shift + P로 구성 편집(UI) 선택

Image

  1. 컴파일러 선택 (C: gcc, C++: g++)

Image

  1. IntelliSense 모드

Image

  1. 설정파일 확인
  • 위 설정한 값들이 아래 json 파일 형태로 저장됨을 확인 Image
  1. 템플릿에서 task.json 파일 만들기
  • 터미널 → 작업 구성 → 템플릿에서 tasks.json 파일 만들기 → Others Image

Image

Image

Image

  1. 코드 복사 수정
  • tasks.json에 아래 내용 복사 붙여넣기
 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
{
    "version": "2.0.0",
    "runner": "terminal",
    "type": "shell",
    "echoCommand": true,
    "presentation" : { "reveal": "always" },
    "tasks": [
          //C++ 컴파일
          {
            "label": "save and compile for C++",
            "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 컴파일
        {
            "label": "save and compile for C",
            "command": "gcc",
            "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
                }
            }
        },
        // 바이너리 실행(Ubuntu)
        {
            "label": "execute",
            "command": "${fileDirname}/${fileBasenameNoExtension}",
            "group": "test"
        }
        // 바이너리 실행(Windows)
        // {
        //     "label": "execute",
        //     "command": "cmd",
        //     "group": "test",
        //     "args": [
        //         "/C", "${fileDirname}\\${fileBasenameNoExtension}"
        //     ]

        // }
    ]
}
  1. 단축키 설정
  • 파일 → 기본 설정 → 바로 가기 키 [Ctrl+K, Ctrl+S] Image

우측 상단 마우스 포인터가 가리키는 아이콘 클릭

Image

빈 파일 확인

Image

아래 내용 복사 붙여넣기

1
2
3
4
5
6
7
8
// 키 바인딩을 이 파일에 넣어서 기본값을 덮어씁니다.
[
    //컴파일
    { "key": "ctrl+alt+c", "command": "workbench.action.tasks.build" },

    //실행
    { "key": "ctrl+alt+r", "command": "workbench.action.tasks.test" }
]

Image

Ctrl + Alt + C

Image

save and compile for C++ 선택

Image

Ctrl + Alt + R

Image

💬 댓글

GitHub 계정으로 로그인하여 댓글을 남겨보세요. GitHub 로그인

🔧 댓글 시스템 설정이 필요합니다

GitHub Discussions 기반 댓글 시스템을 활성화하려면:

  1. Giscus 설정 페이지에서 설정 생성
  2. GISCUS_SETUP_GUIDE.md 파일의 안내를 따라 설정 완료
  3. Repository의 Discussions 기능 활성화

Repository 관리자만 설정할 수 있습니다. 설정이 완료되면 모든 방문자가 댓글을 남길 수 있습니다.