<WSL> VS Code, C/C++ 환경설정
<Pre-work> WSL 환경설정
01. WSL 설치
WSL2 환경설정
[WSL 공식문서], [WSL 개발 환경 설정]Step1. 윈도우에서 WSL 활성화더보기Window + S 단축키를 누르면, 검색 기능이 활성화 됩니다.서검색어로 키워드 입력하면 검색된 를 실행합니다., 체크 ✔ 하여 활
basiclike.tistory.com
02. WSL 초기 설정
WSL2 초기설정
패키지 미러 주소를 한국 카카오 서버로 설정 sudo sed -i 's/archive.ubuntu.com/mirror.kakao.com/g' /etc/apt/sources.list 패키지 업데이트, 업그레이드, 불필요한 패키지 자동 제거 sudo apt update && sudo ap
basiclike.tistory.com
참고) WSL 삭제
WSL2 삭제
1. 터미널 열기 2. 설치된 리눅스 배포판 확인 후 삭제wsl -l -vwsl --unregister Ubuntu-22.04 3. 우분투 배포판 앱 제거 4. 설치된 Linux용 Windows 하위 시스템 제거 [ Window 키 + i ] 단축키▶ App
basiclike.tistory.com
문제가 발생하면, 즉시 삭제하고 처음부터 다시 시작합니다.
Step 1. WSL에 C/C++ 컴파일러(GCC)설치
우분투에 C/C++ 프로그래밍 언어를 사용하기 위해 GCC 컴파일러와 GDB 디버거를 설치합니다.
개발에 필요한 필수 패키지를 설치한다고 생각하시면 됩니다.
01. 데비안 패키지 리스트 업데이드 && 업그레이드
sudo apt-get update && sudo apt-get upgrade -y
02. gcc, gdb 설치
sudo apt-get install build-essential gdb -y
build-essential 패키지는 데비안 계열의 리눅스 플랫폼에서 개발에 필요한 패키지를 묶음을 의미합니다.
build-essential 패키지를 설치하면 GCC, G++ 등 리눅스 개발에 필요한 도구를 설치합니다.
03. gcc 설치 확인
gcc -v
04. gdb 설치 확인
gdb -v
Step 2. Visual Studio Code 설치
우선, Windows 환경에서 VS Code 를 설치합니다.
VS Code 에디터가 설치되어 있다면, Step 2. 단계로 넘어가세요.
01. VS Code 다운로드

02. VS Code 설치
03. VS Code 가 정상적으로 설치되면, 아래 이미지처럼 실행됩니다.

Step 3. Visual Studio Code - Extention 설치
01. WSL Extention 설치
VS Code 를 실행시, 아래 이미지처럼 우측 하단에 WSL Extention 설치 안내가 자동으로 팝업됩니다.

02. 자동으로 WSL Extention 설치 안내가 팝업되지 않는다면, 확장 프로그램 관리에서 검색하여 설치합니다.
VS Code → Extentions → WSL

03. VS Code 에서 WSL 원격 연결


04. VS Code Extentions → C/C++ Extension Pack

05. VS Code 한국어 설정
VS Code 한국어 설정
1. 한국어 팩 설치 2. VS Code 재실행 3. 언어 변경 확인 VS Code 언어 선택 >configure display language
basiclike.tistory.com
06. VS Code 를 종료하고, 다음 단계를 진행하세요.
Step 4. C/C++ 작업 경로 설정
01. C/C++ 소스코드를 작업할 프로젝트 폴더를 생성합니다.
- mkdir = make directory = 프로젝트 폴더 생성 (원하는 폴더명을 사용합니다.)
mkdir projects
- cd = change directory = 경로 이동, 생성한 폴더 경로로 이동합니다.
cd projects


02. C/C++ 소스코드를 작업할 프로젝트 경로로 이동하여 VS code를 실행합니다.





3. C/C++ 테스트 코드를 생성합니다.

HelloWorld1.c 테스트 코드
#include <stdio.h>
int main()
{
printf("Hello, C World!\n");
printf("__STDC_VERSION__ = %ld\n", __STDC_VERSION__);
return 0;
}
HelloWorld2.cpp 테스트 코드
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, C++ World!" << endl;
std::cout << "__cplusplus = " << __cplusplus << std::endl;
return 0;
}
Step 5. C/C++ 사용 환경 설정
C/C++ 의 버전 및 사용할 컴파일러 등을 설정합니다.
C/C++: 구성 편집(UI) 를 사용해 JSON 설정 파일을 생성합니다.
01. 구성 편집(UI) 실행
① Ctrl + Shift + p 단축키를 눌러서 VS Code 명령어 팔렛을 활성화합니다.
② 아래 키워드로 검색하여 < C/C++: 구성 편집(UI) > 을 찾아 선택합니다.
③ UI로 설정한 부분은, json 파일로 자동 변경됩니다.
④ .vscode 폴더의 c_cpp_properties.json 파일에서 확인 할 수 있습니다.


백업된 JSON 설정 파일이 있다면, 구성 편집 선택시 < 구성 편집(JSON) > 으로 작업합니다.
Step 6. C/C++ 빌드 환경 설정
01. 기본 빌드 작업 설정 파일 생성
이전 단계에서 테스트 코드를 빌드 했다면, 아래처럼 tasks.json 파일이 기본값으로 생성됩니다.
tasks.json 파일이 없다면, Ctrl + Shift + P → Tasks: Configure Default Build Task 입력 후 선택합니다.
Tasks: Configure Default Build Task

02. task.json 파일에 아래 빌드 설정 json 코드를 붙여 넣습니다.

{
"version": "2.0.0", // task.json 파일 형식 버전
"runner": "terminal",
"type": "shell", // 터미널 유형: process, shell, cppbuild
"echoCommand": true, // 실행 명령 터미널 출력 여부
"presentation": {
"reveal": "always" // 실행 중 출력 창 항상 표시 여부
},
// 빌드 작업 실행할 목록 작성
"tasks": [
// make Debug directory
{
"label": "mkdir Debug",
"command": "mkdir",
"args": [
"-p", // 하위 경로에 Debug 와 동일 디렉토리 없을때만 생성하는 mkdir 명령 옵션
"Debug"
]
},
// C++ build task
{
"label": "save and build for C++",
"dependsOn":[ // 작업공간 하위 경로에 Debug 폴더 만들기
"mkdir Debug"
],
"dependsOrder": "sequence",
"command": "g++", // G++ (GNU C++ Compiler) 실행 명령
"args": [ // 실행할 명령어에 넘길 매개변수입니다.
"-g",
"${file}",
"-o", // g++ 명령 옵션 output
"${workspaceFolder}/Debug/${fileBasenameNoExtension}"
//, "-std=gnu++11" // (opt) 특정 컴파일러 버전 선택 활성화
],
"options": {
"cwd": "/usr/bin"
},
"group": "build", // 단축키 설정에서 "workbench.action.tasks.build" 의 build 그룹을 의미한다. 아래 C 빌드도 같은 그룹이다.
// workbench.action.tasks.build 그룹이 아닌, g++ 컴파일러를 자동으로 실행시킬 때, 대신 활성화합니다.
//"group": {
// "kind": "build",
// "isDefault": true
//},
"problemMatcher": { // 컴파일러마다 Warning, Error 출력 규칙이 다릅니다.
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
"detail": "wsl g++ build "
},
// C build task
{
"label": "save and build for C",
"dependsOn":[
"mkdir Debug"
],
"dependsOrder": "sequence",
"command": "gcc",
"args": [
"-g",
"${file}",
"-o",
"${workspaceFolder}/Debug/${fileBasenameNoExtension}"
],
"group": "build",
"problemMatcher": {
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
},
// 바이너리 실행(Ubuntu, WSL)
{
"label": "execute",
"command": "${fileDirname}/Debug/${fileBasenameNoExtension}",
"group": "test"
}
]
}
Step 7. 빌드, 실행 단축키 설정
01. [ 빌드, 실행 ] 단축키를 변경합니다.
① Ctrl + Shift + p 단축키를 눌러서 VS Code 명령어 팔렛을 활성화합니다.
② 아래 키워드로 검색하여 선택합니다.
open keyboard shortcuts (JSON)

02. 빌드, 실행 단축키 변경
빌드: ctrl+alt+c 단축키
실행: ctrl+alt+r 단축키
[
// 빌드, tasks.json 파일의 "group": "build"
// 빌드 기본 단축키 : ctrl+shift+b (동일 기능이지만 한손 누르기 힘듦)
{
"key": "ctrl+alt+c",
"command": "workbench.action.tasks.build"
},
// 실행, tasks.json 파일의 "group": "test"
{
"key": "ctrl+alt+r",
"command": "workbench.action.tasks.test"
}
]

03. 기본 터미널 선택
>terminal select default profile

04. VS Code 재실행
변경된 설정을 적용시키기 위해 VSCode를 재실행합니다.
Step 8. C/C++ 빌드, 실행 테스트
01. C 빌드 테스트
① C 언어로 작성된 소스코드에서 Ctrl + Alt+ c 단축키를 눌러서 빌드를 실행합니다.
② Step 6. 에서 작업한 Task.json 파일 설정 중, save and build for C 를 선택합니다.
③ 설정한 빌드 명령어가 동작하여 실행 파일을 생성합니다.
④ C 언어로 작성된 소스코드를 빌드하여 실행 파일이 생성됩니다.

02. C++ 빌드 테스트
① C++ 언어로 작성된 소스코드에서 Ctrl + Alt+ c 단축키를 눌러서 빌드를 실행합니다.
② Step 6. 에서 작업한 Task.json 파일 설정 중, save and build for C++ 을 선택합니다.
③ 설정한 빌드 명령어가 동작하여 실행 파일을 생성합니다.
④ C++ 언어로 작성된 소스코드를 빌드하여 실행 파일이 생성됩니다.

03. C 실행 테스트
C 언어로 작성된 소스코드에서 실행 단축키인 Ctrl + Alt + R 을 사용합니다.
빌드된 파일이 실행됩니다.

04. C++ 실행 테스트
C++ 언어로 작성된 소스코드에서 수정한 실행 단축키인 Ctrl + Alt + R 을 사용합니다.
빌드된 파일이 실행됩니다.

C표준 | __STDC_VERSION__ | C++ 표준 | __cplusplus |
c99, gnu99 | 199901 | C++98 | 199711L |
c11, gnu11 | 201112 | C++11 | 201103L |
C++14 | 201402L | ||
c17, gnu17 | 201710 | C++17 | 201703L |
C++20 | 202002L | ||
C++23 | TBD (202100L) |