Ubuntu 18.04에 Visual Studio Code 설치

Speedwell🍀·2022년 2월 16일
0

Human Following Robot

목록 보기
12/18

1. linux에 맞는 Visual Studio Code 다운받기

https://code.visualstudio.com/download 이 사이트에서 다운받으면 된다.


2. VSC 설치하기

위와 같이 다운받은 VSC를 클릭하면

위와 같이 설치하는 창이 뜬다. 설치를 눌러주면 된다.

이렇게 메뉴에 VSC가 추가된 것을 볼 수 있다.


3. C++ extension for VS Code 설치하기

VSC에서 ctrl + shift + x를 누르면 나오는 Extension 창에서 'C++'을 검색해서 나오는 C/C++ extension을 설치한다.


4. gcc가 설치되어있는지 확인

터미널에 $ gcc -v를 입력하면 gcc 버전을 확인할 수 있다.

만약 gcc가 설치되어있지 않으면 아래의 두 명령어를 통해 설치한다.

$ sudo apt-get update
$ sudo apt-get install build-essential gdb


5. 프로젝트를 만들어보자

먼저 VS Code의 프로젝트를 저장할 폴더를 만들자.

$ mkdir projects
$ cd projects

그 다음 테스트로 helloworld 프로젝트를 만들어보자

$ mkdir helloworld
$ cd helloworld
$ code .

code . 을 입력하면 VS Code가 열린다.


위 사진처럼 초록 네모를 눌러서 새로운 파일을 만들 수 있다. helloworld.cpp 파일을 만들자.

그 다음 아래의 코드들을 붙여넣고 저장한다. 나는 Auto Save를 해주고 싶어서 메뉴창에 있는 File에서 Auto Save를 클릭해줬다.

#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;
}

6. helloworld.cpp 빌드

메뉴에서 Terminal > Configure Default Build Task를 눌러서 C/C++: g++ build active file을 선택한다.

위의 과정을 거치면 tasks.json 파일이 생성된다. 이 task는 g++ compiler가 소스코드로부터 executable file을 생성하도록 한다.

tasks.json의 내용은 아래와 같다.

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "C/C++: g++ 활성 파일 빌드",
			"command": "/usr/bin/g++",
			"args": [
				"-fdiagnostics-color=always",
				"-g",
				"${file}",
				"-o",
				"${fileDirname}/${fileBasenameNoExtension}"
			],
			"options": {
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"detail": "컴파일러: /usr/bin/g++"
		}
	]
}

실행시켜보자!

1) helloworld.cpp 파일로 간다.

2) tasks.json에 defined된 build task를 run하기 위해 Terminal > Run Build Task를 누른다. 아니면 Ctrl + Shift + B를 눌러도 된다.

아래와 같이 터미널이 뜨는 것을 볼 수 있다.

3) 터미널에서 아래 사진처럼 +를 눌러서 새 터미널을 만든 후 ./helloworld를 입력하면 실행된다.


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

0개의 댓글