In this tutorial, I provide the configuration details to setup a simple, modern C++ development environment:
- Visual Studio Code as editor
- Microsoft C++ compiler (msvc)
- C++17
- UTF-encoding source code
- MS Windows 10 OS
Configure Steps
1. Install Tool-sets
- Install Visual Studio Code (version 1.41.1)
- Install C++ extension for VS Code (version 0.26.3)
- Install Microsoft C++ compiler tool-set, i.e., install either of the following tools:
2. Config C++ Project
- create an empty folder, for example, called "project", this is used to store all files for a VS Code project
- create an sub-folder called "debug" under "project" folder, this is used to store compiler producing files
- create an sub-folder called ".vscode" under "project" folder, this is used to store VS Code config files
- create 3 configure files of VS Code under "project/.vscode" folder:
- c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x64/cl.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "${default}"
}
],
"version": 4
}
Note:
- this configure file is for configuring compiler path and IntelliSense settings
- the value of "compilerPath" may be adjusted based on your installation options for MS C++ compiler tool-set accordingly
- the value of "cppStandard" is C++17, this configures VS Code source code editor accordingly
- launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(msvc) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/debug/app.exe",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}/debug",
"environment": [],
"externalConsole": false
}
]
}
Note:
- this configure file is for configuring MS C++ debugger
- the executable file is configured as "app.exe", this may be adjusted based on your project needs accordingly
- tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "msvc build",
"type": "shell",
"command": "cl.exe",
"args": ["/EHsc", "/Zi", "/std:c++17", "/utf-8", "/Fe:", "app.exe", "../*.cpp"],
"options": {
"cwd": "${workspaceRoot}/debug"
},
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "always",
"focus": true
},
"problemMatcher": "$msCompile"
},
{
"label": "clean build",
"type": "shell",
"command": "del",
"args": ["/Q", "*"],
"options": {
"cwd": "${workspaceRoot}/debug"
},
"group": "build",
"presentation": {
"reveal": "always",
"focus": true
},
"problemMatcher": "$msCompile"
},
{
"label": "run build",
"type": "shell",
"command": "app.exe",
"options": {
"cwd": "${workspaceRoot}/debug"
},
"dependsOn": [
"msvc build"
],
"group": {
"kind": "test",
"isDefault": true
},
"presentation": {
"reveal": "always"
}
}
]
}
Note:
- this configure file is for configuring build instructions, including build/clean/run tasks
- the executable file is configured as "app.exe", this may be adjusted based on your project needs accordingly
- quick explanation for the compile arguments of "msvc build" task:
/EHsc - specify the exception handling mode
/Zi - produce a debug build with symbols
/std:c++17 - enable C++17 standard-specific features and behavior explicitly, preferable than /std:c++latest due to long-term maintenance
/utf-8 - set source and execution character sets to UTF-8
/Fe: - specify the executable file name
Work with C++ Project
- start "Developer Command Prompt for VS 2019", this is required for msvc
- from the developer command prompt, navigate into the folder "project", and start VS Code under the folder "project"
> cd project
> code .
- create a C++ source code under the folder "project" with VS Code, for example, let's create the following sample C++ source code "helloworld.cpp"
#include <iostream>
using namespace std;
int main() {
cout << "Hello, world!" << endl;
return 0;
}
- compile C++ code by either one of the following methods:
- press the shortcut key:
ctrl + shift + b
- run
tasks: msvc build
Note:
- The executable file "app.exe" will be generated by msvc under the subfolder "debug" if everything works properly
- different methods to run task in VS Code:
- press the shortcut key:
F1 to access the command palette of VS Code, and then choose the desired task to run
- press the shortcut key:
ctrl + shift + p to access the command palette of VS Code, and then choose the desired task to run
- through the menu of VS Code:
terminal > run task, and then choose the desired task to run
- compile C++ code and then run the executable file with a single task:
- start debugging:
- press the shortcut key:
F5 to access the debugger GUI of VS Code
- clean up all of last build generated files:
References
No comments:
Post a Comment