写在前面的话——vscode有个轻量级特点,初学编程的同学用起来可能会感觉挺好。在windows下用过vs2019系列或者qt的童鞋,在实际做项目时感觉体现不出vscode的优势(多个第三方库的混战实在是配置的头疼)。如果要用,就从一开始摒弃windows系统吧,直接linux;开源的各种项目在linux下的支持更友好。
个人在windows下开发c++较多,因此vscode的开始是c++版本的。vscode的c++细分了不同场合下如何应用:
如果有看官网的习惯,不防直接看英文官网。选择左侧目录栏下的c++即可。
Documentation for Visual Studio Codecode.visualstudio.com特别说明:json文件不允许有注释,下面的注释在项目中需要手动删除
安装内容
安装过程忽略,自行百度。
vscode扩展在vscode安装好之后,左侧菜单栏最后一项打开,搜索以下内容进行安装
其他可选扩展:
mingw版本的vscode启动方式:打开cmd,输入"code ."打开vscode
// 作用是 build cpp 和 running the build cpp
main menu --> Terminal --> configure default build task 自动创建tasks.json
编译:
如果只编译某个cpp,则切换到需要编译的文件下,执行编译命令,并产生exe;如果编译全部cpp,则切换到任意cpp文件下,执行编译命令,并产生exe
编译命令:Ctrl+Shift+B 或者main menu --> Terminal --> Run Build task
编译完成之后会在当前文件夹下产生一个***.exe;这个exe的名称可以在tasks.json中的“args”中指定
运行编译好的exe文件:
main menu --> Terminal --> New Terminal
dir
.***.exe
效果如下:
PS D:VS-Code-CplusplusHELLOWORLD> dir
目录: D:VS-Code-CplusplusHELLOWORLD
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2020/01/17 11:39 .vscode
-a---- 2020/01/17 10:50 291 helloworld.cpp
-a---- 2020/01/17 11:09 149497 helloworld.exe
-a---- 2020/01/17 11:47 149497 myProgram.exe
PS D:VS-Code-CplusplusHELLOWORLD> .myProgram.exe
Hello C++ World from VS Code and the C++ extension!
PS D:VS-Code-CplusplusHELLOWORLD>
// 作用是 press F5 to debug the program
main menu --> Debug --> Add configuration --> C++(GDB/ILDB) 自动创建launch.json
不知道为什么我的vscode没有下拉选项“Choose g++.exe build and debug active file.” 只能手动修改
需要修改"program" 和 "miDebuggerPath"
且configurations中多了一项"preLaunchTask": "g++.exe build active file"
这一条的作用:调试前重新编译一遍
开始调试
A. 不设断点,停在程序开始位置:"stopAtEntry": true
B. 设置断点,停在断点位置:"stopAtEntry": false
1. 切换到cpp文件 (即active file)
2. Press F5 or from the main menu choose Debug > Start Debugging.
which will allow you to change settings such as the path to the compiler, include paths, C++ standard (default is C++17), and more.
运行C/C++:Edit configurations(UI):
Ctrl+Shift+P 打开控制窗口,输入"C/C++" --> Choose C/C++:Edit configurations(UI)
结果:在.vscode文件夹下自动生成c_cpp_properties.json文件
添加头文件(当需要的头文件不在工作区或者不在标准库路径时):
修改"includePath"选项
比如添加opencv的头文件
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"${vcpkgRoot}/x86-windows/include",
"E:opencv_4.1.1opencvbuildinclude" ,
"E:opencv_4.1.1opencvbuildincludeopencv2"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "E:MinGWbingcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x86"
}
],
"version": 4
}
那么添加opencv的库文件在哪里呢?网上有说在tasks.json下的args参数下,分别按照-I -L -l去设置
"args": [
"-I", "E:opencv_4.1.1opencvbuildinclude",
"-L", "E:opencv_4.1.1opencvbuild***", //这里因为是windows下使用了mingw,所以必须是mingw版本的opencv
//如果是linux系统下,后面的输出文件后缀就要从exe改为o
"-l", "opencv_world411",
"-g",
"${workspaceFolder}*.cpp", //"${file}", 只编译当前文件改为编译文件夹内的所有cpp文件
"-o",
"${workspaceFolder}myProgram.exe"//"${fileDirname}${fileBasenameNoExtension}.exe"
],
mingw版本的opencv下载地址:https://github.com/huihut/OpenCV-MinGW-Build
当创建一个新的工作区时,只需要拷贝之前的.vscode文件夹到新的工作区,做适当修改即可开始coding!
只列出与mingw版本不同的地方
安装内容
To use MSVC in VS Code, you must start VS Code from a Developer Command Prompt for Visual Studio.
命令如下:
mkdir projects
cd projects
mkdir helloworld
cd helloworld
code .
// 作用是 build cpp 和 running the build cpp
Ctrl+Shift+P 打开控制窗口,输入"C/C++" --> Choose C/C++:Edit configurations(UI)
修改compiler path为
E:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105binHostx64x64cl.exe
修改IntelliSense mode为msvc-x64
添加opencv外部依赖库类似于mingw
main menu --> View --> Command Palette
--> "task" --> Tasks: Configure Default Build Task
--> Create tasks.json file from template
--> Others
替换全部文件内容:
{
"version": "2.0.0",
"tasks": [
{
"label": "msvc build",
"type": "shell",
"command": "cl.exe",
"args": ["/EHsc", //specifying the exception handling mode (EHsc)
"/Zi", //produce a debug build with symbols (Zi)
"/Fe:", "helloworld.exe", //tells the compiler to name the executable "helloworld.exe"
"helloworld.cpp"],
"group": {
"kind": "build",
"isDefault": true // run when you press Ctrl+Shift+B
},
"presentation": {
"reveal": "always"
},
"problemMatcher": "$msCompile"
}
]
}
// 作用是 press F5 to debug the program
main menu --> Debug --> Add configuration --> C/C++ Windows(Launch) 自动创建launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(msvc) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/helloworld.exe",
"args": [],
"stopAtEntry": true, //F5在main的第一行创建一个断点并主动停下,否则停在手动断点处
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false
}
]
}
注意:再次强调编译之前需要把文件切换到某个cpp
Ctrl+Shift+B
or
View > Command Palette and start typing "Tasks: Run Build Task"
编译出错:
Note: If you see an error message that looks like this: cl.exe: command not found, it means you have not started VS Code from a Developer Command Prompt for VS.
Press F5 or from the main menu choose Debug > Start Debugging
变量监视
手动添加代码中的变量到watch中,实时监视变量状态
c_cpp_propotites.json
{
"configurations": [
{
"name": "x64",
"includePath": [
"${workspaceFolder}/**",
"${vcpkgRoot}/x86-windows/include",
"E:opencv_4.1.1opencvbuildinclude",
"E:opencv_4.1.1opencvbuildincludeopencv2"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "E:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105binHostx64x64cl.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "msvc build",
"type": "shell",
"command": "cl.exe",
"args": [
"-I","E:opencv_4.1.1opencvbuildinclude",
"E:opencv_4.1.1opencvbuildx64vc15libopencv_world411.lib",
"/EHsc",
"/Zi",
"/Fe:", "helloworld.exe",
"${workspaceFolder}*.cpp" //"helloworld.cpp" "${workspaceFolder}*.cpp"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "always"
},
"problemMatcher": "$msCompile"
}
]
}