250x250
Notice
Recent Posts
«   2024/07   »
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
관리 메뉴

일상 코딩

[Linux / Ubuntu] VSCode C++ Linux 환경 설정 본문

C++/Linux 환경 설정

[Linux / Ubuntu] VSCode C++ Linux 환경 설정

polarcompass 2021. 12. 9. 14:06
728x90
VSCODE 다운로드 통한 설치

https://code.visualstudio.com/#alt-downloads

 

Visual Studio Code - Code Editing. Redefined

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

code.visualstudio.com

VSCODE 터미널 통한 설치
$ sudo apt update
$ sudo apt install software-properties-common apt-transport-https wget
$ sudo apt install code
C++ extension 설치
Extension 단축키 - Ctrl+Shift+X

Extension에서 C++ 검색 후 설치

GCC 설치 확인
# 설치 확인
$ gcc -v

# 설치 안되어 있을시
$ sudo apt-get update
$ sudo apt-get install build-essential gdb

 

C++ 로 개발할 폴더 생성
$ mkdir projects
$ cd projects
$ mkdir helloworld
$ cd helloworld
$ code .

폴더 이름은 'projects', 'hello_world', 'cpp' 등등 본인이 원하는 이름으로 생성한다.

* 처음 셋팅한 이 폴더에서 앞으로 계속 작업을 할것이므로 원하는 이름을 적도록한다.

앞으로의 설정 다음 세개의 파일을 셋팅하는 것을 목표로 한다.

  •  tasks.json  - complier build settings
  •  launch.json - debugger settings
  •  c_cpp_properties.json  - complier path and IntelliSense settings

이후에 새로운 폴더를 생성하면 위의 세개 파일을 복사해서 붙여넣으면 동일한 세팅값으로

개발이 가능하다.

파일 하나를 생성한다.

파일제목.cpp 파일 생성한다.

testfile.cpp로 파일 하나 만들어 준 후 아래 코드를 복붙한다.

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

    for (const string& word : msg)
    {
        cout << word << " ";
    }
    cout << endl;
}

 

Build testile.cpp

이제 tasks.json 파일을 생성 후 testfile.cpp를 build 할 것이다.

우선 Terminal > Configure Default Build Task 로 들어가 준다.

            (터미널 > 기본 빌드 작업 구성...)

C/C++ clang++ build active file 선택해 준다.

tasks.json 가 생성되면 아래 코드를 복붙한다.

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "g++ build active file",
      "command": "/usr/bin/g++",
      "args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
      "options": {
        "cwd": "/usr/bin"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

 

Running testfile.cpp

이제 testfile.cpp 파일로 돌아와서 Cmd+Shift+B   파일을 build 해준다.

 

CodeLLDB 디버깅 Extension 파일 설치

CodeLLDB

Debug testfile.cpp

다음은 launch.json 을 만들어준다. Fn + F5 눌러 debug를 실행해준다.

혹은 메인 메뉴에서 Run > Add Configuration...을 선택 후 

C++ (GDB/LLDB) 선택

그 다음 다음과 같은 선택지를 보게 된다.

g++ build and debug active file

그 후  launch.json  파일이 생성 된 후 아래와 같은 코드 복붙한다.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "g++ build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "g++ build active file",
      "miDebuggerPath": "/usr/bin/gdb"
    }
  ]
}

※ 주의점

tasks.json 의 label 부분과

"label": "clang++ build active file",

launch.json의 preLaunchTask 부분 이름이 같아야 한다.

"preLaunchTask": "clang++ build active file"

한글로 되어있을 경우엔 되도록 영어로 바꿔 주도록 한다.

C/C++ configuration 세팅

c_cpp_properties.json 파일 세팅 해준다.

Ctrl+Shift+P 단축키로 Command Palette 열어준다.

C/C++:Edit Configurations (UI) 로 UI 형태로 세팅하거나

C/C++:Edit Configurations (JSON) 으로 아래 코드를 복붙 해준다.

{
  "configurations": [
    {
      "name": "Linux",
      "includePath": ["${workspaceFolder}/**"],
      "defines": [],
      "compilerPath": "/usr/bin/gcc",
      "cStandard": "c11",
      "cppStandard": "c++17",
      "intelliSenseMode": "clang-x64"
    }
  ],
  "version": 4
}

 

출처

https://code.visualstudio.com/docs/cpp/config-linux

 

Get Started with C++ on Linux in Visual Studio Code

Configure the C++ extension in Visual Studio Code to target g++ and GDB on Linux

code.visualstudio.com

 

728x90