오랜만에 C++ 기반의 프로젝트를 수행하게 되었는데 윈도우와 우분투 모든 환경에서 진행이 가능하도록 하고 싶은 욕심에 연습을 위해 VSCode를 이용하여 C++ 개발 환경 구축을 진행하였고 방법에 대한 기록이다.
1. C++ extension for VSCode
기본적으로 VSCode에서 C++을 개발하기 위한 확장판을 설치해야 한다.
아래의 공식 페이지에서 C++ 뿐만 아니라 GCC on Window, GCC on Linux 내용이 모두 설명되어 있다.
설정 내용은 크게 다르지 않다.
Configure Visual Studio Code for Microsoft C++
Configure Visual Studio Code for Microsoft C++
Configure the C++ extension in Visual Studio Code to target Microsoft C++ on Windows.
code.visualstudio.com
2. 빌드 환경 구축하기
윈도우와 리눅스 환경 별로 사용하는 컴파일러와 링커는 다르다.
윈도우 환경에서는 visual studio 에서 제공하는 컴파일러 cl.exe 를 사용하나 리눅스의 경우는 gcc를 사용한다.
이런 차이로 컴파일러 별로 빌드와 디버거 설정에 필요한 tasks.json 과 launch.json 파일을 별도로 작성해야한다.
개발 환경 선호도에 따라 gcc와 cl을 모두 설정하고 싶은 경우에는 합쳐서 사용하면 된다. 다만, launch.json에 정의된 디버깅 옵션은 실행할때에 선택이 가능하지만 빌드옵션의 경우는 "Configure Default Build Task" 를 통해 그때마다 변경해야 한다.
deafult 의 설정은 tasks.json 파일의 "group" 키의 isDefault:true 설정 여부로 확인할 수 있다.
3. Intellisence 설정하기
Intellisence는 c_cpp_configuration.json 에서 설정한다. 자세한 정보는 아래에서 확인할 수 있다.
Configure IntelliSense for C++ cross-compilation (visualstudio.com)
Configure IntelliSense for C++ cross-compilation
Configure Visual Studio Code c_cpp_properties.json to get IntelliSense when you are compiling for a different platform
code.visualstudio.com
우선 Intellisence의 활용도로 보면 include header 파일의 경로가 안찾아진다고 나오는 경우 해당 파일의 경로를 추가하면 된다. 하지만 이것은 단순히 intellisence 기능을 위한 것일 뿐 실제로 빌드에는 여향을 주는 것이 아니므로
필요한 경로가 있다면 tasks.json의 args 에 해당 정보를 입력해야 한다.
VSCode 개발환경을 설정하면서 프로젝트를 github에 연결하고 윈도우와 리눅스 환경에서 모두 사용할 수 있는 설정을 구성했다. 시스템 환경의 경로에 따라 일부 다를 수 있겠으나 사용자 환경에 맞춰서 쉽게 변경 사용할 수 있을 것 이다.
<c_cpp_properties.json>
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64",
"compileCommands": "${workspaceFolder}/build/compile_commands.json",
"configurationProvider": "ms-vscode.cmake-tools"
},
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "C:/msys64/mingw64/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
<launch.json>
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "cl.exe build and debug active file",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"preLaunchTask": "cl.exe build active file"
},
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
},
{
"name": "g++ - Build and debug active file in Linux",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file in Linux",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
<tasks.json>
{
"version": "2.0.0",
"windows": {
"options": {
"shell": {
"executable": "cmd.exe",
"args": [
"/C",
// The path to VsDevCmd.bat depends on the version of Visual Studio you have installed.
"\"C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/Tools/VsDevCmd.bat\"",
"&&"
]
}
}
},
"tasks": [
{
"type": "shell",
"label": "cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"/I",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"problemMatcher": [
"$msCompile"
],
"group": "build",
"detail": "compiler : cl.exe"
},
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:/msys64/mingw64/bin/g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"detail": "compiler: C:/msys64/mingw64/bin/g++.exe",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"type": "cppbuild",
"label": "C/C++: g++ build active file in Linux",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"detail": "compiler: /usr/bin/g++"
}
]
}